micro509 0.12.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/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/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/oids2.js +9 -2
- package/dist/internal/asn1/oids2.js.map +1 -1
- package/dist/internal/shared/dn.js +142 -12
- 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 +85 -94
- 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/revocation/chain.js +110 -55
- package/dist/revocation/chain.js.map +1 -1
- package/dist/revocation/crl.js.map +1 -1
- package/dist/revocation/ocsp.js +1 -1
- 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/package.json +32 -22
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { DerElement, ReadRootElementOptions, ReadSequenceChildrenOptions } from "../internal/asn1/der.js";
|
|
2
|
+
import { DerBitString } from "../internal/asn1/asn1.js";
|
|
3
|
+
import { ErrorResult, Micro509Error } from "../result/result.js";
|
|
4
|
+
//#region src/der/der.d.ts
|
|
5
|
+
/** Machine-readable failure reason for the DER readers and decoders. */
|
|
6
|
+
type DecodeDerErrorCode = "malformed";
|
|
7
|
+
/** Structured failure payload for DER reading and decoding. */
|
|
8
|
+
interface DecodeDerFailure extends Micro509Error<DecodeDerErrorCode> {
|
|
9
|
+
/** Always `false` for failures. */
|
|
10
|
+
readonly ok: false;
|
|
11
|
+
}
|
|
12
|
+
/** Success-or-failure result from a DER reader or decoder. */
|
|
13
|
+
type DecodeDerResult<TValue> = {
|
|
14
|
+
readonly ok: true;
|
|
15
|
+
readonly value: TValue;
|
|
16
|
+
} | ErrorResult<DecodeDerErrorCode, Record<never, never>, DecodeDerFailure>;
|
|
17
|
+
/**
|
|
18
|
+
* Reads one TLV element from {@linkcode bytes} starting at {@linkcode offset}.
|
|
19
|
+
*
|
|
20
|
+
* @param offset Byte position of the tag octet. Defaults to 0.
|
|
21
|
+
* @throws if the element is truncated, indefinite-length, or non-minimally encoded.
|
|
22
|
+
*/
|
|
23
|
+
declare function readDerElementOrThrow(bytes: Uint8Array, offset?: number): DerElement;
|
|
24
|
+
/**
|
|
25
|
+
* Reads one TLV element from {@linkcode bytes} starting at {@linkcode offset}.
|
|
26
|
+
*
|
|
27
|
+
* @param offset Byte position of the tag octet. Defaults to 0.
|
|
28
|
+
*/
|
|
29
|
+
declare function readDerElement(bytes: Uint8Array, offset?: number): DecodeDerResult<DerElement>;
|
|
30
|
+
/**
|
|
31
|
+
* Reads the single top-level TLV element from {@linkcode bytes}.
|
|
32
|
+
*
|
|
33
|
+
* @throws if {@linkcode bytes} carries trailing data, or nesting exceeds the depth guard.
|
|
34
|
+
*/
|
|
35
|
+
declare function readDerRootOrThrow(bytes: Uint8Array, options?: ReadRootElementOptions): DerElement;
|
|
36
|
+
/** Reads the single top-level TLV element from {@linkcode bytes}. */
|
|
37
|
+
declare function readDerRoot(bytes: Uint8Array, options?: ReadRootElementOptions): DecodeDerResult<DerElement>;
|
|
38
|
+
/**
|
|
39
|
+
* Reads a DER-encoded SEQUENCE from {@linkcode bytes} and returns its direct children.
|
|
40
|
+
*
|
|
41
|
+
* @throws if the root element is not a SEQUENCE, or if child boundaries are inconsistent.
|
|
42
|
+
*/
|
|
43
|
+
declare function readDerSequenceOrThrow(bytes: Uint8Array, options?: ReadSequenceChildrenOptions): DerElement[];
|
|
44
|
+
/** Reads a DER-encoded SEQUENCE from {@linkcode bytes} and returns its direct children. */
|
|
45
|
+
declare function readDerSequence(bytes: Uint8Array, options?: ReadSequenceChildrenOptions): DecodeDerResult<DerElement[]>;
|
|
46
|
+
/**
|
|
47
|
+
* Reads the direct children of a constructed {@linkcode parent} within {@linkcode source}.
|
|
48
|
+
*
|
|
49
|
+
* @throws if a child overflows {@linkcode parent}, or data is left between the last child and its end.
|
|
50
|
+
*/
|
|
51
|
+
declare function derChildrenOrThrow(source: Uint8Array, parent: DerElement): DerElement[];
|
|
52
|
+
/** Reads the direct children of a constructed {@linkcode parent} within {@linkcode source}. */
|
|
53
|
+
declare function derChildren(source: Uint8Array, parent: DerElement): DecodeDerResult<DerElement[]>;
|
|
54
|
+
/**
|
|
55
|
+
* Decodes a BOOLEAN element (tag `0x01`).
|
|
56
|
+
*
|
|
57
|
+
* @throws if {@linkcode element} is mis-tagged, or holds anything but `0x00` or `0xff`.
|
|
58
|
+
*/
|
|
59
|
+
declare function decodeDerBooleanOrThrow(element: DerElement): boolean;
|
|
60
|
+
/** Decodes a BOOLEAN element (tag `0x01`). */
|
|
61
|
+
declare function decodeDerBoolean(element: DerElement): DecodeDerResult<boolean>;
|
|
62
|
+
/**
|
|
63
|
+
* Decodes an INTEGER element (tag `0x02`) into a `number`.
|
|
64
|
+
*
|
|
65
|
+
* @throws if {@linkcode element} is mis-tagged, negative, non-minimally encoded,
|
|
66
|
+
* or exceeds {@linkcode Number.MAX_SAFE_INTEGER}.
|
|
67
|
+
*/
|
|
68
|
+
declare function decodeDerIntegerOrThrow(element: DerElement): number;
|
|
69
|
+
/** Decodes an INTEGER element (tag `0x02`) into a `number`. */
|
|
70
|
+
declare function decodeDerInteger(element: DerElement): DecodeDerResult<number>;
|
|
71
|
+
/**
|
|
72
|
+
* Decodes an OCTET STRING element (tag `0x04`) into its payload bytes.
|
|
73
|
+
*
|
|
74
|
+
* @throws if {@linkcode element} is mis-tagged.
|
|
75
|
+
*/
|
|
76
|
+
declare function decodeDerOctetStringOrThrow(element: DerElement): Uint8Array;
|
|
77
|
+
/** Decodes an OCTET STRING element (tag `0x04`) into its payload bytes. */
|
|
78
|
+
declare function decodeDerOctetString(element: DerElement): DecodeDerResult<Uint8Array>;
|
|
79
|
+
/**
|
|
80
|
+
* Decodes an OBJECT IDENTIFIER element (tag `0x06`) into dotted-decimal form.
|
|
81
|
+
*
|
|
82
|
+
* @throws if {@linkcode element} is mis-tagged, or holds a malformed sub-identifier.
|
|
83
|
+
*/
|
|
84
|
+
declare function decodeDerOidOrThrow(element: DerElement): string;
|
|
85
|
+
/** Decodes an OBJECT IDENTIFIER element (tag `0x06`) into dotted-decimal form. */
|
|
86
|
+
declare function decodeDerOid(element: DerElement): DecodeDerResult<string>;
|
|
87
|
+
/**
|
|
88
|
+
* Decodes a BIT STRING element (tag `0x03`) into a {@linkcode DerBitString}.
|
|
89
|
+
*
|
|
90
|
+
* Unused trailing bits are returned as encoded, with a violation of {@linkcode https://www.itu.int/rec/T-REC-X.690-202102-I/en | X.690 §11.2.1} reported
|
|
91
|
+
* through {@linkcode DerBitString.nonZeroPadding}, since certificates in the wild carry them.
|
|
92
|
+
*
|
|
93
|
+
* @throws if {@linkcode element} is mis-tagged, or claims more than seven unused bits.
|
|
94
|
+
*/
|
|
95
|
+
declare function decodeDerBitStringOrThrow(element: DerElement): DerBitString;
|
|
96
|
+
/**
|
|
97
|
+
* Decodes a BIT STRING element (tag `0x03`) into a {@linkcode DerBitString}.
|
|
98
|
+
*
|
|
99
|
+
* Unused trailing bits are returned as encoded, with a violation of {@linkcode https://www.itu.int/rec/T-REC-X.690-202102-I/en | X.690 §11.2.1} reported
|
|
100
|
+
* through {@linkcode DerBitString.nonZeroPadding}, since certificates in the wild carry them.
|
|
101
|
+
*/
|
|
102
|
+
declare function decodeDerBitString(element: DerElement): DecodeDerResult<DerBitString>;
|
|
103
|
+
/**
|
|
104
|
+
* Decodes a string element into text, dispatching on its tag.
|
|
105
|
+
*
|
|
106
|
+
* Supports UTF8String, PrintableString, IA5String, UniversalString, and BMPString.
|
|
107
|
+
*
|
|
108
|
+
* @throws on TeletexString, and on every other string tag.
|
|
109
|
+
*/
|
|
110
|
+
declare function decodeDerStringOrThrow(element: DerElement): string;
|
|
111
|
+
/**
|
|
112
|
+
* Decodes a string element into text, dispatching on its tag.
|
|
113
|
+
*
|
|
114
|
+
* Supports UTF8String, PrintableString, IA5String, UniversalString, and BMPString.
|
|
115
|
+
*/
|
|
116
|
+
declare function decodeDerString(element: DerElement): DecodeDerResult<string>;
|
|
117
|
+
/**
|
|
118
|
+
* Decodes a UTCTime (tag `0x17`) or GeneralizedTime (tag `0x18`) element into a {@linkcode Date}.
|
|
119
|
+
*
|
|
120
|
+
* @throws if {@linkcode element} carries any other tag, or a malformed time value.
|
|
121
|
+
*/
|
|
122
|
+
declare function decodeDerTimeOrThrow(element: DerElement): Date;
|
|
123
|
+
/** Decodes a UTCTime (tag `0x17`) or GeneralizedTime (tag `0x18`) element into a {@linkcode Date}. */
|
|
124
|
+
declare function decodeDerTime(element: DerElement): DecodeDerResult<Date>;
|
|
125
|
+
//#endregion
|
|
126
|
+
export { DecodeDerErrorCode, DecodeDerFailure, DecodeDerResult, decodeDerBitString, decodeDerBitStringOrThrow, decodeDerBoolean, decodeDerBooleanOrThrow, decodeDerInteger, decodeDerIntegerOrThrow, decodeDerOctetString, decodeDerOctetStringOrThrow, decodeDerOid, decodeDerOidOrThrow, decodeDerString, decodeDerStringOrThrow, decodeDerTime, decodeDerTimeOrThrow, derChildren, derChildrenOrThrow, readDerElement, readDerElementOrThrow, readDerRoot, readDerRootOrThrow, readDerSequence, readDerSequenceOrThrow };
|
|
127
|
+
//# sourceMappingURL=der.d.ts.map
|
package/dist/der/der.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { readElement as e, readRootElement as t, readSequenceChildren as n } from "../internal/asn1/der.js";
|
|
2
|
+
import { childrenOf as r, decodeBitString as i, decodeBoolean as a, decodeIntegerNumber as o, decodeObjectIdentifier as s, decodeString as c, parseTime as l } from "../internal/asn1/asn1.js";
|
|
3
|
+
import { failureResult as u, rethrowIfInvariant as d, successResult as f } from "../result/result.js";
|
|
4
|
+
//#region src/der/der.ts
|
|
5
|
+
function attempt(e, t) {
|
|
6
|
+
try {
|
|
7
|
+
return f(e());
|
|
8
|
+
} catch (e) {
|
|
9
|
+
return d(e), u("malformed", e instanceof Error ? e.message : t);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function requireTag(e, t, n) {
|
|
13
|
+
if (e.tag !== t) throw Error(`Expected ${n}`);
|
|
14
|
+
return e;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Reads one TLV element from {@linkcode bytes} starting at {@linkcode offset}.
|
|
18
|
+
*
|
|
19
|
+
* @param offset Byte position of the tag octet. Defaults to 0.
|
|
20
|
+
* @throws if the element is truncated, indefinite-length, or non-minimally encoded.
|
|
21
|
+
*/
|
|
22
|
+
function readDerElementOrThrow(t, n = 0) {
|
|
23
|
+
return e(t, n);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Reads one TLV element from {@linkcode bytes} starting at {@linkcode offset}.
|
|
27
|
+
*
|
|
28
|
+
* @param offset Byte position of the tag octet. Defaults to 0.
|
|
29
|
+
*/
|
|
30
|
+
function readDerElement(e, t = 0) {
|
|
31
|
+
return attempt(() => readDerElementOrThrow(e, t), "Malformed DER element");
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Reads the single top-level TLV element from {@linkcode bytes}.
|
|
35
|
+
*
|
|
36
|
+
* @throws if {@linkcode bytes} carries trailing data, or nesting exceeds the depth guard.
|
|
37
|
+
*/
|
|
38
|
+
function readDerRootOrThrow(e, n) {
|
|
39
|
+
return t(e, n);
|
|
40
|
+
}
|
|
41
|
+
/** Reads the single top-level TLV element from {@linkcode bytes}. */
|
|
42
|
+
function readDerRoot(e, t) {
|
|
43
|
+
return attempt(() => readDerRootOrThrow(e, t), "Malformed DER element");
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Reads a DER-encoded SEQUENCE from {@linkcode bytes} and returns its direct children.
|
|
47
|
+
*
|
|
48
|
+
* @throws if the root element is not a SEQUENCE, or if child boundaries are inconsistent.
|
|
49
|
+
*/
|
|
50
|
+
function readDerSequenceOrThrow(e, t) {
|
|
51
|
+
return n(e, t);
|
|
52
|
+
}
|
|
53
|
+
/** Reads a DER-encoded SEQUENCE from {@linkcode bytes} and returns its direct children. */
|
|
54
|
+
function readDerSequence(e, t) {
|
|
55
|
+
return attempt(() => readDerSequenceOrThrow(e, t), "Malformed DER sequence");
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Reads the direct children of a constructed {@linkcode parent} within {@linkcode source}.
|
|
59
|
+
*
|
|
60
|
+
* @throws if a child overflows {@linkcode parent}, or data is left between the last child and its end.
|
|
61
|
+
*/
|
|
62
|
+
function derChildrenOrThrow(e, t) {
|
|
63
|
+
return r(e, t);
|
|
64
|
+
}
|
|
65
|
+
/** Reads the direct children of a constructed {@linkcode parent} within {@linkcode source}. */
|
|
66
|
+
function derChildren(e, t) {
|
|
67
|
+
return attempt(() => derChildrenOrThrow(e, t), "Malformed DER container");
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Decodes a BOOLEAN element (tag `0x01`).
|
|
71
|
+
*
|
|
72
|
+
* @throws if {@linkcode element} is mis-tagged, or holds anything but `0x00` or `0xff`.
|
|
73
|
+
*/
|
|
74
|
+
function decodeDerBooleanOrThrow(e) {
|
|
75
|
+
return a(requireTag(e, 1, "BOOLEAN").value);
|
|
76
|
+
}
|
|
77
|
+
/** Decodes a BOOLEAN element (tag `0x01`). */
|
|
78
|
+
function decodeDerBoolean(e) {
|
|
79
|
+
return attempt(() => decodeDerBooleanOrThrow(e), "Malformed BOOLEAN");
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Decodes an INTEGER element (tag `0x02`) into a `number`.
|
|
83
|
+
*
|
|
84
|
+
* @throws if {@linkcode element} is mis-tagged, negative, non-minimally encoded,
|
|
85
|
+
* or exceeds {@linkcode Number.MAX_SAFE_INTEGER}.
|
|
86
|
+
*/
|
|
87
|
+
function decodeDerIntegerOrThrow(e) {
|
|
88
|
+
return o(requireTag(e, 2, "INTEGER").value);
|
|
89
|
+
}
|
|
90
|
+
/** Decodes an INTEGER element (tag `0x02`) into a `number`. */
|
|
91
|
+
function decodeDerInteger(e) {
|
|
92
|
+
return attempt(() => decodeDerIntegerOrThrow(e), "Malformed INTEGER");
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Decodes an OCTET STRING element (tag `0x04`) into its payload bytes.
|
|
96
|
+
*
|
|
97
|
+
* @throws if {@linkcode element} is mis-tagged.
|
|
98
|
+
*/
|
|
99
|
+
function decodeDerOctetStringOrThrow(e) {
|
|
100
|
+
return requireTag(e, 4, "OCTET STRING").value;
|
|
101
|
+
}
|
|
102
|
+
/** Decodes an OCTET STRING element (tag `0x04`) into its payload bytes. */
|
|
103
|
+
function decodeDerOctetString(e) {
|
|
104
|
+
return attempt(() => decodeDerOctetStringOrThrow(e), "Malformed OCTET STRING");
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Decodes an OBJECT IDENTIFIER element (tag `0x06`) into dotted-decimal form.
|
|
108
|
+
*
|
|
109
|
+
* @throws if {@linkcode element} is mis-tagged, or holds a malformed sub-identifier.
|
|
110
|
+
*/
|
|
111
|
+
function decodeDerOidOrThrow(e) {
|
|
112
|
+
return s(requireTag(e, 6, "OBJECT IDENTIFIER").value);
|
|
113
|
+
}
|
|
114
|
+
/** Decodes an OBJECT IDENTIFIER element (tag `0x06`) into dotted-decimal form. */
|
|
115
|
+
function decodeDerOid(e) {
|
|
116
|
+
return attempt(() => decodeDerOidOrThrow(e), "Malformed OBJECT IDENTIFIER");
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Decodes a BIT STRING element (tag `0x03`) into a {@linkcode DerBitString}.
|
|
120
|
+
*
|
|
121
|
+
* Unused trailing bits are returned as encoded, with a violation of {@linkcode https://www.itu.int/rec/T-REC-X.690-202102-I/en | X.690 §11.2.1} reported
|
|
122
|
+
* through {@linkcode DerBitString.nonZeroPadding}, since certificates in the wild carry them.
|
|
123
|
+
*
|
|
124
|
+
* @throws if {@linkcode element} is mis-tagged, or claims more than seven unused bits.
|
|
125
|
+
*/
|
|
126
|
+
function decodeDerBitStringOrThrow(e) {
|
|
127
|
+
return i(e);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Decodes a BIT STRING element (tag `0x03`) into a {@linkcode DerBitString}.
|
|
131
|
+
*
|
|
132
|
+
* Unused trailing bits are returned as encoded, with a violation of {@linkcode https://www.itu.int/rec/T-REC-X.690-202102-I/en | X.690 §11.2.1} reported
|
|
133
|
+
* through {@linkcode DerBitString.nonZeroPadding}, since certificates in the wild carry them.
|
|
134
|
+
*/
|
|
135
|
+
function decodeDerBitString(e) {
|
|
136
|
+
return attempt(() => decodeDerBitStringOrThrow(e), "Malformed BIT STRING");
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Decodes a string element into text, dispatching on its tag.
|
|
140
|
+
*
|
|
141
|
+
* Supports UTF8String, PrintableString, IA5String, UniversalString, and BMPString.
|
|
142
|
+
*
|
|
143
|
+
* @throws on TeletexString, and on every other string tag.
|
|
144
|
+
*/
|
|
145
|
+
function decodeDerStringOrThrow(e) {
|
|
146
|
+
return c(e.tag, e.value);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Decodes a string element into text, dispatching on its tag.
|
|
150
|
+
*
|
|
151
|
+
* Supports UTF8String, PrintableString, IA5String, UniversalString, and BMPString.
|
|
152
|
+
*/
|
|
153
|
+
function decodeDerString(e) {
|
|
154
|
+
return attempt(() => decodeDerStringOrThrow(e), "Malformed string");
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Decodes a UTCTime (tag `0x17`) or GeneralizedTime (tag `0x18`) element into a {@linkcode Date}.
|
|
158
|
+
*
|
|
159
|
+
* @throws if {@linkcode element} carries any other tag, or a malformed time value.
|
|
160
|
+
*/
|
|
161
|
+
function decodeDerTimeOrThrow(e) {
|
|
162
|
+
return l(e);
|
|
163
|
+
}
|
|
164
|
+
/** Decodes a UTCTime (tag `0x17`) or GeneralizedTime (tag `0x18`) element into a {@linkcode Date}. */
|
|
165
|
+
function decodeDerTime(e) {
|
|
166
|
+
return attempt(() => decodeDerTimeOrThrow(e), "Malformed time");
|
|
167
|
+
}
|
|
168
|
+
//#endregion
|
|
169
|
+
export { decodeDerBitString, decodeDerBitStringOrThrow, decodeDerBoolean, decodeDerBooleanOrThrow, decodeDerInteger, decodeDerIntegerOrThrow, decodeDerOctetString, decodeDerOctetStringOrThrow, decodeDerOid, decodeDerOidOrThrow, decodeDerString, decodeDerStringOrThrow, decodeDerTime, decodeDerTimeOrThrow, derChildren, derChildrenOrThrow, readDerElement, readDerElementOrThrow, readDerRoot, readDerRootOrThrow, readDerSequence, readDerSequenceOrThrow };
|
|
170
|
+
|
|
171
|
+
//# sourceMappingURL=der.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"der.js","names":[],"sources":["../../src/der/der.ts"],"sourcesContent":["/**\n * Element-oriented DER readers and decoders.\n *\n * Each decoder validates the element's tag before reading its value, so a\n * mis-tagged element fails instead of yielding a wrong value.\n *\n * The `OrThrow` variants throw on malformed input.\n * The plain variants return a {@linkcode DecodeDerResult}.\n *\n * @module\n */\n\nimport type { DerBitString } from '#micro509/internal/asn1/asn1';\nimport {\n\tchildrenOf,\n\tdecodeBitString,\n\tdecodeBoolean,\n\tdecodeIntegerNumber,\n\tdecodeObjectIdentifier,\n\tdecodeString,\n\tparseTime,\n} from '#micro509/internal/asn1/asn1';\nimport type {\n\tDerElement,\n\tReadRootElementOptions,\n\tReadSequenceChildrenOptions,\n} from '#micro509/internal/asn1/der';\nimport { readElement, readRootElement, readSequenceChildren } from '#micro509/internal/asn1/der';\nimport type { ErrorResult, Micro509Error } from '#micro509/result/result';\nimport { failureResult, rethrowIfInvariant, successResult } from '#micro509/result/result';\n\n/** Machine-readable failure reason for the DER readers and decoders. */\nexport type DecodeDerErrorCode = 'malformed';\n\n/** Structured failure payload for DER reading and decoding. */\nexport interface DecodeDerFailure extends Micro509Error<DecodeDerErrorCode> {\n\t/** Always `false` for failures. */\n\treadonly ok: false;\n}\n\n/** Success-or-failure result from a DER reader or decoder. */\nexport type DecodeDerResult<TValue> =\n\t| { readonly ok: true; readonly value: TValue }\n\t| ErrorResult<DecodeDerErrorCode, Record<never, never>, DecodeDerFailure>;\n\nfunction attempt<TValue>(decode: () => TValue, fallback: string): DecodeDerResult<TValue> {\n\ttry {\n\t\treturn successResult(decode());\n\t} catch (error) {\n\t\trethrowIfInvariant(error);\n\t\treturn failureResult('malformed', error instanceof Error ? error.message : fallback);\n\t}\n}\n\nfunction requireTag(element: DerElement, tag: number, label: string): DerElement {\n\tif (element.tag !== tag) {\n\t\tthrow new Error(`Expected ${label}`);\n\t}\n\treturn element;\n}\n\n/**\n * Reads one TLV element from {@linkcode bytes} starting at {@linkcode offset}.\n *\n * @param offset Byte position of the tag octet. Defaults to 0.\n * @throws if the element is truncated, indefinite-length, or non-minimally encoded.\n */\nexport function readDerElementOrThrow(bytes: Uint8Array, offset = 0): DerElement {\n\treturn readElement(bytes, offset);\n}\n\n/**\n * Reads one TLV element from {@linkcode bytes} starting at {@linkcode offset}.\n *\n * @param offset Byte position of the tag octet. Defaults to 0.\n */\nexport function readDerElement(bytes: Uint8Array, offset = 0): DecodeDerResult<DerElement> {\n\treturn attempt(() => readDerElementOrThrow(bytes, offset), 'Malformed DER element');\n}\n\n/**\n * Reads the single top-level TLV element from {@linkcode bytes}.\n *\n * @throws if {@linkcode bytes} carries trailing data, or nesting exceeds the depth guard.\n */\nexport function readDerRootOrThrow(\n\tbytes: Uint8Array,\n\toptions?: ReadRootElementOptions,\n): DerElement {\n\treturn readRootElement(bytes, options);\n}\n\n/** Reads the single top-level TLV element from {@linkcode bytes}. */\nexport function readDerRoot(\n\tbytes: Uint8Array,\n\toptions?: ReadRootElementOptions,\n): DecodeDerResult<DerElement> {\n\treturn attempt(() => readDerRootOrThrow(bytes, options), 'Malformed DER element');\n}\n\n/**\n * Reads a DER-encoded SEQUENCE from {@linkcode bytes} and returns its direct children.\n *\n * @throws if the root element is not a SEQUENCE, or if child boundaries are inconsistent.\n */\nexport function readDerSequenceOrThrow(\n\tbytes: Uint8Array,\n\toptions?: ReadSequenceChildrenOptions,\n): DerElement[] {\n\treturn readSequenceChildren(bytes, options);\n}\n\n/** Reads a DER-encoded SEQUENCE from {@linkcode bytes} and returns its direct children. */\nexport function readDerSequence(\n\tbytes: Uint8Array,\n\toptions?: ReadSequenceChildrenOptions,\n): DecodeDerResult<DerElement[]> {\n\treturn attempt(() => readDerSequenceOrThrow(bytes, options), 'Malformed DER sequence');\n}\n\n/**\n * Reads the direct children of a constructed {@linkcode parent} within {@linkcode source}.\n *\n * @throws if a child overflows {@linkcode parent}, or data is left between the last child and its end.\n */\nexport function derChildrenOrThrow(source: Uint8Array, parent: DerElement): DerElement[] {\n\treturn childrenOf(source, parent);\n}\n\n/** Reads the direct children of a constructed {@linkcode parent} within {@linkcode source}. */\nexport function derChildren(source: Uint8Array, parent: DerElement): DecodeDerResult<DerElement[]> {\n\treturn attempt(() => derChildrenOrThrow(source, parent), 'Malformed DER container');\n}\n\n/**\n * Decodes a BOOLEAN element (tag `0x01`).\n *\n * @throws if {@linkcode element} is mis-tagged, or holds anything but `0x00` or `0xff`.\n */\nexport function decodeDerBooleanOrThrow(element: DerElement): boolean {\n\treturn decodeBoolean(requireTag(element, 0x01, 'BOOLEAN').value);\n}\n\n/** Decodes a BOOLEAN element (tag `0x01`). */\nexport function decodeDerBoolean(element: DerElement): DecodeDerResult<boolean> {\n\treturn attempt(() => decodeDerBooleanOrThrow(element), 'Malformed BOOLEAN');\n}\n\n/**\n * Decodes an INTEGER element (tag `0x02`) into a `number`.\n *\n * @throws if {@linkcode element} is mis-tagged, negative, non-minimally encoded,\n * or exceeds {@linkcode Number.MAX_SAFE_INTEGER}.\n */\nexport function decodeDerIntegerOrThrow(element: DerElement): number {\n\treturn decodeIntegerNumber(requireTag(element, 0x02, 'INTEGER').value);\n}\n\n/** Decodes an INTEGER element (tag `0x02`) into a `number`. */\nexport function decodeDerInteger(element: DerElement): DecodeDerResult<number> {\n\treturn attempt(() => decodeDerIntegerOrThrow(element), 'Malformed INTEGER');\n}\n\n/**\n * Decodes an OCTET STRING element (tag `0x04`) into its payload bytes.\n *\n * @throws if {@linkcode element} is mis-tagged.\n */\nexport function decodeDerOctetStringOrThrow(element: DerElement): Uint8Array {\n\treturn requireTag(element, 0x04, 'OCTET STRING').value;\n}\n\n/** Decodes an OCTET STRING element (tag `0x04`) into its payload bytes. */\nexport function decodeDerOctetString(element: DerElement): DecodeDerResult<Uint8Array> {\n\treturn attempt(() => decodeDerOctetStringOrThrow(element), 'Malformed OCTET STRING');\n}\n\n/**\n * Decodes an OBJECT IDENTIFIER element (tag `0x06`) into dotted-decimal form.\n *\n * @throws if {@linkcode element} is mis-tagged, or holds a malformed sub-identifier.\n */\nexport function decodeDerOidOrThrow(element: DerElement): string {\n\treturn decodeObjectIdentifier(requireTag(element, 0x06, 'OBJECT IDENTIFIER').value);\n}\n\n/** Decodes an OBJECT IDENTIFIER element (tag `0x06`) into dotted-decimal form. */\nexport function decodeDerOid(element: DerElement): DecodeDerResult<string> {\n\treturn attempt(() => decodeDerOidOrThrow(element), 'Malformed OBJECT IDENTIFIER');\n}\n\n/**\n * Decodes a BIT STRING element (tag `0x03`) into a {@linkcode DerBitString}.\n *\n * Unused trailing bits are returned as encoded, with a violation of {@linkcode https://www.itu.int/rec/T-REC-X.690-202102-I/en | X.690 §11.2.1} reported\n * through {@linkcode DerBitString.nonZeroPadding}, since certificates in the wild carry them.\n *\n * @throws if {@linkcode element} is mis-tagged, or claims more than seven unused bits.\n */\nexport function decodeDerBitStringOrThrow(element: DerElement): DerBitString {\n\treturn decodeBitString(element);\n}\n\n/**\n * Decodes a BIT STRING element (tag `0x03`) into a {@linkcode DerBitString}.\n *\n * Unused trailing bits are returned as encoded, with a violation of {@linkcode https://www.itu.int/rec/T-REC-X.690-202102-I/en | X.690 §11.2.1} reported\n * through {@linkcode DerBitString.nonZeroPadding}, since certificates in the wild carry them.\n */\nexport function decodeDerBitString(element: DerElement): DecodeDerResult<DerBitString> {\n\treturn attempt(() => decodeDerBitStringOrThrow(element), 'Malformed BIT STRING');\n}\n\n/**\n * Decodes a string element into text, dispatching on its tag.\n *\n * Supports UTF8String, PrintableString, IA5String, UniversalString, and BMPString.\n *\n * @throws on TeletexString, and on every other string tag.\n */\nexport function decodeDerStringOrThrow(element: DerElement): string {\n\treturn decodeString(element.tag, element.value);\n}\n\n/**\n * Decodes a string element into text, dispatching on its tag.\n *\n * Supports UTF8String, PrintableString, IA5String, UniversalString, and BMPString.\n */\nexport function decodeDerString(element: DerElement): DecodeDerResult<string> {\n\treturn attempt(() => decodeDerStringOrThrow(element), 'Malformed string');\n}\n\n/**\n * Decodes a UTCTime (tag `0x17`) or GeneralizedTime (tag `0x18`) element into a {@linkcode Date}.\n *\n * @throws if {@linkcode element} carries any other tag, or a malformed time value.\n */\nexport function decodeDerTimeOrThrow(element: DerElement): Date {\n\treturn parseTime(element);\n}\n\n/** Decodes a UTCTime (tag `0x17`) or GeneralizedTime (tag `0x18`) element into a {@linkcode Date}. */\nexport function decodeDerTime(element: DerElement): DecodeDerResult<Date> {\n\treturn attempt(() => decodeDerTimeOrThrow(element), 'Malformed time');\n}\n"],"mappings":";;;;AA6CA,SAAS,QAAgB,GAAsB,GAA2C;CACzF,IAAI;EACH,OAAO,EAAc,EAAO,CAAC;CAC9B,SAAS,GAAO;EAEf,OADA,EAAmB,CAAK,GACjB,EAAc,aAAa,aAAiB,QAAQ,EAAM,UAAU,CAAQ;CACpF;AACD;AAEA,SAAS,WAAW,GAAqB,GAAa,GAA2B;CAChF,IAAI,EAAQ,QAAQ,GACnB,MAAU,MAAM,YAAY,GAAO;CAEpC,OAAO;AACR;;;;;;;AAQA,SAAgB,sBAAsB,GAAmB,IAAS,GAAe;CAChF,OAAO,EAAY,GAAO,CAAM;AACjC;;;;;;AAOA,SAAgB,eAAe,GAAmB,IAAS,GAAgC;CAC1F,OAAO,cAAc,sBAAsB,GAAO,CAAM,GAAG,uBAAuB;AACnF;;;;;;AAOA,SAAgB,mBACf,GACA,GACa;CACb,OAAO,EAAgB,GAAO,CAAO;AACtC;;AAGA,SAAgB,YACf,GACA,GAC8B;CAC9B,OAAO,cAAc,mBAAmB,GAAO,CAAO,GAAG,uBAAuB;AACjF;;;;;;AAOA,SAAgB,uBACf,GACA,GACe;CACf,OAAO,EAAqB,GAAO,CAAO;AAC3C;;AAGA,SAAgB,gBACf,GACA,GACgC;CAChC,OAAO,cAAc,uBAAuB,GAAO,CAAO,GAAG,wBAAwB;AACtF;;;;;;AAOA,SAAgB,mBAAmB,GAAoB,GAAkC;CACxF,OAAO,EAAW,GAAQ,CAAM;AACjC;;AAGA,SAAgB,YAAY,GAAoB,GAAmD;CAClG,OAAO,cAAc,mBAAmB,GAAQ,CAAM,GAAG,yBAAyB;AACnF;;;;;;AAOA,SAAgB,wBAAwB,GAA8B;CACrE,OAAO,EAAc,WAAW,GAAS,GAAM,SAAS,CAAC,CAAC,KAAK;AAChE;;AAGA,SAAgB,iBAAiB,GAA+C;CAC/E,OAAO,cAAc,wBAAwB,CAAO,GAAG,mBAAmB;AAC3E;;;;;;;AAQA,SAAgB,wBAAwB,GAA6B;CACpE,OAAO,EAAoB,WAAW,GAAS,GAAM,SAAS,CAAC,CAAC,KAAK;AACtE;;AAGA,SAAgB,iBAAiB,GAA8C;CAC9E,OAAO,cAAc,wBAAwB,CAAO,GAAG,mBAAmB;AAC3E;;;;;;AAOA,SAAgB,4BAA4B,GAAiC;CAC5E,OAAO,WAAW,GAAS,GAAM,cAAc,CAAC,CAAC;AAClD;;AAGA,SAAgB,qBAAqB,GAAkD;CACtF,OAAO,cAAc,4BAA4B,CAAO,GAAG,wBAAwB;AACpF;;;;;;AAOA,SAAgB,oBAAoB,GAA6B;CAChE,OAAO,EAAuB,WAAW,GAAS,GAAM,mBAAmB,CAAC,CAAC,KAAK;AACnF;;AAGA,SAAgB,aAAa,GAA8C;CAC1E,OAAO,cAAc,oBAAoB,CAAO,GAAG,6BAA6B;AACjF;;;;;;;;;AAUA,SAAgB,0BAA0B,GAAmC;CAC5E,OAAO,EAAgB,CAAO;AAC/B;;;;;;;AAQA,SAAgB,mBAAmB,GAAoD;CACtF,OAAO,cAAc,0BAA0B,CAAO,GAAG,sBAAsB;AAChF;;;;;;;;AASA,SAAgB,uBAAuB,GAA6B;CACnE,OAAO,EAAa,EAAQ,KAAK,EAAQ,KAAK;AAC/C;;;;;;AAOA,SAAgB,gBAAgB,GAA8C;CAC7E,OAAO,cAAc,uBAAuB,CAAO,GAAG,kBAAkB;AACzE;;;;;;AAOA,SAAgB,qBAAqB,GAA2B;CAC/D,OAAO,EAAU,CAAO;AACzB;;AAGA,SAAgB,cAAc,GAA4C;CACzE,OAAO,cAAc,qBAAqB,CAAO,GAAG,gBAAgB;AACrE"}
|
package/dist/der.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { 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 } from "./internal/asn1/der.js";
|
|
2
|
+
import { DerBitString, hexToBytes, toHex } from "./internal/asn1/asn1.js";
|
|
3
|
+
import { DecodeDerErrorCode, DecodeDerFailure, DecodeDerResult, decodeDerBitString, decodeDerBitStringOrThrow, decodeDerBoolean, decodeDerBooleanOrThrow, decodeDerInteger, decodeDerIntegerOrThrow, decodeDerOctetString, decodeDerOctetStringOrThrow, decodeDerOid, decodeDerOidOrThrow, decodeDerString, decodeDerStringOrThrow, decodeDerTime, decodeDerTimeOrThrow, derChildren, derChildrenOrThrow, readDerElement, readDerElementOrThrow, readDerRoot, readDerRootOrThrow, readDerSequence, readDerSequenceOrThrow } from "./der/der.js";
|
|
4
|
+
export { DEFAULT_MAX_DER_DEPTH, type DecodeDerErrorCode, type DecodeDerFailure, type DecodeDerResult, type DerBitString, type DerElement, type ReadRootElementOptions, type ReadSequenceChildrenOptions, assertDerMaxDepth, concatBytes, decodeDerBitString, decodeDerBitStringOrThrow, decodeDerBoolean, decodeDerBooleanOrThrow, decodeDerInteger, decodeDerIntegerOrThrow, decodeDerOctetString, decodeDerOctetStringOrThrow, decodeDerOid, decodeDerOidOrThrow, decodeDerString, decodeDerStringOrThrow, decodeDerTime, decodeDerTimeOrThrow, bitString as derBitString, bmpString as derBmpString, bool as derBoolean, derChildren, derChildrenOrThrow, explicitContext as derExplicitContext, generalizedTime as derGeneralizedTime, ia5String as derIa5String, implicitConstructedContext as derImplicitConstructedContext, implicitPrimitiveContext as derImplicitPrimitiveContext, integer as derInteger, integerFromNumber as derIntegerFromNumber, nullValue as derNull, octetString as derOctetString, objectIdentifier as derOid, printableString as derPrintableString, sequence as derSequence, setOf as derSet, time as derTime, tlv as derTlv, universalString as derUniversalString, utcTime as derUtcTime, utf8String as derUtf8String, hexToBytes, readDerElement, readDerElementOrThrow, readDerRoot, readDerRootOrThrow, readDerSequence, readDerSequenceOrThrow, toHex };
|
package/dist/der.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { DEFAULT_MAX_DER_DEPTH as e, assertDerMaxDepth as t, bitString as n, bmpString as r, bool as i, concatBytes as a, explicitContext as o, generalizedTime as s, ia5String as c, implicitConstructedContext as l, implicitPrimitiveContext as u, integer as d, integerFromNumber as f, nullValue as p, objectIdentifier as m, octetString as h, printableString as g, sequence as _, setOf as v, time as y, tlv as b, universalString as x, utcTime as S, utf8String as C } from "./internal/asn1/der.js";
|
|
2
|
+
import { hexToBytes as w, toHex as T } from "./internal/asn1/asn1.js";
|
|
3
|
+
import { decodeDerBitString as E, decodeDerBitStringOrThrow as D, decodeDerBoolean as O, decodeDerBooleanOrThrow as k, decodeDerInteger as A, decodeDerIntegerOrThrow as j, decodeDerOctetString as M, decodeDerOctetStringOrThrow as N, decodeDerOid as P, decodeDerOidOrThrow as F, decodeDerString as I, decodeDerStringOrThrow as L, decodeDerTime as R, decodeDerTimeOrThrow as z, derChildren as B, derChildrenOrThrow as V, readDerElement as H, readDerElementOrThrow as U, readDerRoot as W, readDerRootOrThrow as G, readDerSequence as K, readDerSequenceOrThrow as q } from "./der/der.js";
|
|
4
|
+
export { e as DEFAULT_MAX_DER_DEPTH, t as assertDerMaxDepth, a as concatBytes, E as decodeDerBitString, D as decodeDerBitStringOrThrow, O as decodeDerBoolean, k as decodeDerBooleanOrThrow, A as decodeDerInteger, j as decodeDerIntegerOrThrow, M as decodeDerOctetString, N as decodeDerOctetStringOrThrow, P as decodeDerOid, F as decodeDerOidOrThrow, I as decodeDerString, L as decodeDerStringOrThrow, R as decodeDerTime, z as decodeDerTimeOrThrow, n as derBitString, r as derBmpString, i as derBoolean, B as derChildren, V as derChildrenOrThrow, o as derExplicitContext, s as derGeneralizedTime, c as derIa5String, l as derImplicitConstructedContext, u as derImplicitPrimitiveContext, d as derInteger, f as derIntegerFromNumber, p as derNull, h as derOctetString, m as derOid, g as derPrintableString, _ as derSequence, v as derSet, y as derTime, b as derTlv, x as derUniversalString, S as derUtcTime, C as derUtf8String, w as hexToBytes, H as readDerElement, U as readDerElementOrThrow, W as readDerRoot, G as readDerRootOrThrow, K as readDerSequence, q as readDerSequenceOrThrow, T as toHex };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/internal/asn1/asn1.d.ts
|
|
2
|
+
/** Converts raw bytes to a lowercase hex string with no separator. */
|
|
3
|
+
declare function toHex(bytes: Uint8Array): string;
|
|
4
|
+
/** A BIT STRING payload and the number of unused trailing bits in its final byte. */
|
|
5
|
+
interface DerBitString {
|
|
6
|
+
/** Payload bytes, excluding the leading unused-bit count octet. */
|
|
7
|
+
readonly bytes: Uint8Array;
|
|
8
|
+
/** Unused trailing bits in the final byte, 0 through 7. */
|
|
9
|
+
readonly unusedBits: number;
|
|
10
|
+
/** `true` when the original encoding had non-zero padding bits (DER violation). */
|
|
11
|
+
readonly nonZeroPadding: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Converts a hex string (even or odd length) to a {@linkcode Uint8Array}.
|
|
15
|
+
* Odd-length strings are left-padded with a zero nibble.
|
|
16
|
+
*/
|
|
17
|
+
declare function hexToBytes(value: string): Uint8Array;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { DerBitString, hexToBytes, toHex };
|
|
20
|
+
//# sourceMappingURL=asn1.d.ts.map
|
|
@@ -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,KAA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gCAAgC;CAEjD,IAAI,IAAQ;CACZ,KAAK,IAAI,IAAQ,GAAG,IAAQ,EAAM,QAAQ,KAAS,GAAG;EACrD,IAAM,IAAQ,EAAM,IACd,IAAS,EAAM,IAAQ,IACvB,IAAQ,EAAM,IAAQ,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"}
|
|
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 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)) {\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;;;;;;;AAQjC,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,SA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