@ac-kit/format-asn1 0.2.1 → 0.3.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/CHANGELOG.md +119 -0
- package/README.md +65 -7
- package/dist/_encoding/def-tags.d.ts +69 -0
- package/dist/_encoding/def-tags.d.ts.map +1 -0
- package/dist/_encoding/def-tags.js +239 -0
- package/dist/_encoding/def-tags.js.map +1 -0
- package/dist/_encoding/default-output.d.ts +9 -0
- package/dist/_encoding/default-output.d.ts.map +1 -0
- package/dist/_encoding/default-output.js +43 -0
- package/dist/_encoding/default-output.js.map +1 -0
- package/dist/_encoding/default-wire-value.d.ts +9 -0
- package/dist/_encoding/default-wire-value.d.ts.map +1 -0
- package/dist/_encoding/default-wire-value.js +56 -0
- package/dist/_encoding/default-wire-value.js.map +1 -0
- package/dist/_encoding/tag.d.ts +6 -1
- package/dist/_encoding/tag.d.ts.map +1 -1
- package/dist/_encoding/tag.js +10 -2
- package/dist/_encoding/tag.js.map +1 -1
- package/dist/_encoding/values.d.ts +70 -8
- package/dist/_encoding/values.d.ts.map +1 -1
- package/dist/_encoding/values.js +302 -72
- package/dist/_encoding/values.js.map +1 -1
- package/dist/ber/decode.d.ts.map +1 -1
- package/dist/ber/decode.js +25 -34
- package/dist/ber/decode.js.map +1 -1
- package/dist/ber/encode.js +1 -0
- package/dist/ber/encode.js.map +1 -1
- package/dist/cer/decode.d.ts.map +1 -1
- package/dist/cer/decode.js +26 -29
- package/dist/cer/decode.js.map +1 -1
- package/dist/cer/encode.d.ts +3 -0
- package/dist/cer/encode.d.ts.map +1 -1
- package/dist/cer/encode.js +52 -5
- package/dist/cer/encode.js.map +1 -1
- package/dist/cer.d.ts +1 -1
- package/dist/cer.d.ts.map +1 -1
- package/dist/cer.js +1 -1
- package/dist/cer.js.map +1 -1
- package/dist/der/decode.d.ts +57 -2
- package/dist/der/decode.d.ts.map +1 -1
- package/dist/der/decode.js +581 -139
- package/dist/der/decode.js.map +1 -1
- package/dist/der/default-encoding.d.ts +4 -0
- package/dist/der/default-encoding.d.ts.map +1 -0
- package/dist/der/default-encoding.js +25 -0
- package/dist/der/default-encoding.js.map +1 -0
- package/dist/der/encode.d.ts +20 -6
- package/dist/der/encode.d.ts.map +1 -1
- package/dist/der/encode.js +122 -115
- package/dist/der/encode.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/per/decode.d.ts.map +1 -1
- package/dist/per/decode.js +16 -6
- package/dist/per/decode.js.map +1 -1
- package/dist/per/encode.d.ts +3 -0
- package/dist/per/encode.d.ts.map +1 -1
- package/dist/per/encode.js +31 -13
- package/dist/per/encode.js.map +1 -1
- package/dist/per.d.ts +1 -1
- package/dist/per.d.ts.map +1 -1
- package/dist/per.js +1 -1
- package/dist/per.js.map +1 -1
- package/dist/schema/_lazy-resolution.d.ts +9 -0
- package/dist/schema/_lazy-resolution.d.ts.map +1 -0
- package/dist/schema/_lazy-resolution.js +15 -0
- package/dist/schema/_lazy-resolution.js.map +1 -0
- package/dist/schema/errors.d.ts +7 -0
- package/dist/schema/errors.d.ts.map +1 -0
- package/dist/schema/errors.js +10 -0
- package/dist/schema/errors.js.map +1 -0
- package/dist/schema/types/base.d.ts +1 -1
- package/dist/schema/types/base.d.ts.map +1 -1
- package/dist/schema/types/constructed/choice.d.ts +2 -2
- package/dist/schema/types/constructed/choice.d.ts.map +1 -1
- package/dist/schema/types/constructed/choice.js.map +1 -1
- package/dist/schema/types/constructed/component.d.ts +60 -18
- package/dist/schema/types/constructed/component.d.ts.map +1 -1
- package/dist/schema/types/constructed/component.js +15 -2
- package/dist/schema/types/constructed/component.js.map +1 -1
- package/dist/schema/types/constructed/sequence-of.d.ts +2 -2
- package/dist/schema/types/constructed/sequence-of.d.ts.map +1 -1
- package/dist/schema/types/constructed/sequence.d.ts +2 -2
- package/dist/schema/types/constructed/sequence.d.ts.map +1 -1
- package/dist/schema/types/constructed/sequence.js.map +1 -1
- package/dist/schema/types/constructed/set-of.d.ts +2 -2
- package/dist/schema/types/constructed/set-of.d.ts.map +1 -1
- package/dist/schema/types/constructed/set.d.ts +2 -2
- package/dist/schema/types/constructed/set.d.ts.map +1 -1
- package/dist/schema/types/constructed/set.js.map +1 -1
- package/dist/schema/types/primitives/lazy.d.ts +6 -6
- package/dist/schema/types/primitives/lazy.d.ts.map +1 -1
- package/dist/schema/types/primitives/lazy.js.map +1 -1
- package/dist/schema/validate.d.ts +4 -1
- package/dist/schema/validate.d.ts.map +1 -1
- package/dist/schema/validate.js +4 -1
- package/dist/schema/validate.js.map +1 -1
- package/dist/schema/values.d.ts +6 -0
- package/dist/schema/values.d.ts.map +1 -1
- package/dist/schema/walk.d.ts +38 -5
- package/dist/schema/walk.d.ts.map +1 -1
- package/dist/schema/walk.js +89 -52
- package/dist/schema/walk.js.map +1 -1
- package/package.json +6 -4
package/dist/der/decode.js
CHANGED
|
@@ -1,88 +1,376 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { bigIntFromBytesBe, bytesIsEqual, compareBytes, decodeText, setRecordEntry, } from "@ac-kit/core";
|
|
2
|
+
import { choiceAlternatives, effectiveTaggingMode, isConstructedEncoding, outerTagSet, outerTagSetHas, unwrapDef, } from "../_encoding/def-tags.js";
|
|
3
|
+
import { freshDefault } from "../_encoding/default-output.js";
|
|
2
4
|
import { DecodingError } from "../_encoding/errors.js";
|
|
3
|
-
import {
|
|
4
|
-
import { readTlv } from "../_encoding/tlv.js";
|
|
5
|
+
import { tagSortKey } from "../_encoding/tlv.js";
|
|
5
6
|
import { decodeGeneralizedTime, decodeOid, decodeReal, decodeRelativeOid, decodeUtcTime, } from "../_encoding/values.js";
|
|
6
7
|
import { universalTagFor } from "../ber.js";
|
|
8
|
+
import { SchemaDefinitionError } from "../schema/errors.js";
|
|
9
|
+
import { defaultEncoding } from "./default-encoding.js";
|
|
7
10
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
11
|
+
* Deepest nesting of decoded values `derDecode` follows before refusing the
|
|
12
|
+
* input. Only a recursive (`lazy`) schema lets the input choose its depth; this
|
|
13
|
+
* keeps such input from exhausting the call stack.
|
|
14
|
+
*/
|
|
15
|
+
const MAX_DEPTH = 512;
|
|
16
|
+
// Largest tag number that can take one more base-128 digit and stay safe.
|
|
17
|
+
const MAX_TAG_PREFIX = Math.floor((Number.MAX_SAFE_INTEGER - 0x7f) / 0x80);
|
|
18
|
+
const TAG_CLASSES = [
|
|
19
|
+
"universal",
|
|
20
|
+
"application",
|
|
21
|
+
"context",
|
|
22
|
+
"private",
|
|
23
|
+
];
|
|
24
|
+
/**
|
|
25
|
+
* DER decoder (X.690 §10, §11): decodes exactly one value of `def` from
|
|
26
|
+
* `bytes`.
|
|
27
|
+
*
|
|
28
|
+
* Always refused, as malformed BER as well: truncated input; the indefinite
|
|
29
|
+
* length form (§10.1; use `berDecode` for BER input); the reserved length octet
|
|
30
|
+
* `0xFF`; a high tag number written with a leading `0x80` octet or a tag number
|
|
31
|
+
* below 31 in the long form (§8.1.2); a primitive/constructed bit that does not
|
|
32
|
+
* match the type (§8.1.2.5, §10.2: DER strings are primitive, EXPLICIT tags
|
|
33
|
+
* constructed); bytes left over inside an EXPLICIT tag or at the end of a
|
|
34
|
+
* non-extensible SEQUENCE or SET; a BOOLEAN that is not one octet; an empty
|
|
35
|
+
* INTEGER or ENUMERATED; a BIT STRING without its initial octet, with more than
|
|
36
|
+
* 7 unused bits or empty with unused bits; a non-empty NULL; a BMPString or
|
|
37
|
+
* UniversalString whose length is not a multiple of 2 or 4; a UTCTime or
|
|
38
|
+
* GeneralizedTime that is not syntactically valid or names a date or time that
|
|
39
|
+
* does not exist; a SET with an unknown, duplicate or missing component.
|
|
40
|
+
*
|
|
41
|
+
* Refused in strict mode (the default) only, being canonical-form rules:
|
|
42
|
+
* trailing bytes after the value; a length not in the fewest octets (§10.1); a
|
|
43
|
+
* BOOLEAN other than `0x00`/`0xFF` (§11.1); an INTEGER or ENUMERATED with a
|
|
44
|
+
* redundant leading octet (§8.3.2); BIT STRING unused bits not zero (§11.2.1)
|
|
45
|
+
* or, for a type with named bits, trailing zero bits (§11.2.2); a component
|
|
46
|
+
* encoded although equal to its known wire DEFAULT (§11.5); SET components out
|
|
47
|
+
* of tag order (§10.3); SET OF elements out of order (§11.6); a UTCTime other
|
|
48
|
+
* than `YYMMDDHHMMSSZ` (§11.8); a GeneralizedTime other than
|
|
49
|
+
* `YYYYMMDDHHMMSS[.f]Z` with no trailing zero in the fraction (§11.7).
|
|
50
|
+
*
|
|
51
|
+
* An absent OPTIONAL or DEFAULT component is recognised by one tag of lookahead
|
|
52
|
+
* against the set of tags its type may start with — an untagged CHOICE
|
|
53
|
+
* contributes the tags of all its alternatives, nested CHOICEs included. An
|
|
54
|
+
* untagged ANY or open type has no finite tag set, so an OPTIONAL one is taken
|
|
55
|
+
* as present whenever an element remains: such a component must be the last of
|
|
56
|
+
* its SEQUENCE (as X.680 §25.6 requires anyway). An absent transformed DEFAULT
|
|
57
|
+
* is cloned from the declared output without running its transform.
|
|
58
|
+
*
|
|
59
|
+
* Complexity: O(n) in the input size, plus the DER encoding of each DEFAULT
|
|
60
|
+
* value once per component def in strict mode. Nesting deeper than 512 values
|
|
61
|
+
* is refused.
|
|
62
|
+
*
|
|
63
|
+
* @param def - Schema of the value.
|
|
64
|
+
* @param bytes - One DER encoding.
|
|
65
|
+
* @param strict - `false` relaxes the canonical-form rules listed above, for
|
|
66
|
+
* input from encoders that are BER-valid but not DER-exact.
|
|
67
|
+
* @returns The decoded value. ANY values keep their original bytes.
|
|
68
|
+
* @throws {DecodingError} On every violation listed above, with the absolute
|
|
69
|
+
* offset of the offending element, or if an absent output-only DEFAULT cannot
|
|
70
|
+
* be structured-cloned.
|
|
10
71
|
*/
|
|
11
72
|
export function derDecode(def, bytes, strict = true) {
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (def.kind === "lazy")
|
|
17
|
-
return derDecodeReader(def.getter(), reader, strict);
|
|
18
|
-
if (def.kind === "transform") {
|
|
19
|
-
const decoded = derDecodeReader(def.innerType, reader, strict);
|
|
20
|
-
const fn = def.fn ?? ((v) => v);
|
|
21
|
-
return fn(decoded);
|
|
22
|
-
}
|
|
23
|
-
if (def.kind === "choice")
|
|
24
|
-
return decodeChoice(def, reader, strict);
|
|
25
|
-
if (def.kind === "tagged")
|
|
26
|
-
return decodeTagged(def, reader, strict);
|
|
27
|
-
if (def.kind === "any") {
|
|
28
|
-
const { tag, contents } = readTlv(reader);
|
|
29
|
-
const tagBytes = encodeTag(tag);
|
|
30
|
-
const lenBytes = encodeDefiniteLength(contents.length);
|
|
31
|
-
return { encoded: concatBytes(tagBytes, lenBytes, contents) };
|
|
32
|
-
}
|
|
33
|
-
const { tag, contents } = readTlv(reader);
|
|
34
|
-
validateTag(def, tag, reader.position);
|
|
35
|
-
return derDecodeContents(def, contents, strict);
|
|
36
|
-
}
|
|
37
|
-
function validateTag(def, tag, pos) {
|
|
38
|
-
const expected = universalTagFor(def);
|
|
39
|
-
if (!expected)
|
|
40
|
-
return;
|
|
41
|
-
if (tag.tagClass !== "universal" || tag.tagNumber !== expected.tagNumber) {
|
|
42
|
-
throw new DecodingError(`DER: expected UNIVERSAL ${expected.tagNumber}, got ${tag.tagClass} ${tag.tagNumber}`, pos);
|
|
73
|
+
const context = { bytes, strict };
|
|
74
|
+
const element = readElement(context, 0, bytes.length);
|
|
75
|
+
if (strict && element.end !== bytes.length) {
|
|
76
|
+
throw new DecodingError(`DER: ${bytes.length - element.end} byte(s) after the value`, element.end);
|
|
43
77
|
}
|
|
78
|
+
return decodeValue(context, def, element, 0);
|
|
44
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Decodes DER contents octets — the value part of a TLV whose identifier and
|
|
82
|
+
* length were already read — as `def`, with the same rules as {@link derDecode}.
|
|
83
|
+
*
|
|
84
|
+
* @param def - Schema of the value; a CHOICE has no contents of its own and is
|
|
85
|
+
* refused.
|
|
86
|
+
* @param contents - The contents octets only.
|
|
87
|
+
* @param strict - See {@link derDecode}.
|
|
88
|
+
* @throws {DecodingError} As {@link derDecode}; offsets are relative to
|
|
89
|
+
* `contents`.
|
|
90
|
+
*/
|
|
45
91
|
export function derDecodeContents(def, contents, strict) {
|
|
92
|
+
const context = {
|
|
93
|
+
bytes: contents,
|
|
94
|
+
strict,
|
|
95
|
+
};
|
|
96
|
+
const constructed = isConstructedEncoding(def, decodingError) ?? false;
|
|
97
|
+
return decodeContents(context, def, {
|
|
98
|
+
tag: { tagClass: "universal", tagNumber: 0, constructed },
|
|
99
|
+
start: 0,
|
|
100
|
+
contentStart: 0,
|
|
101
|
+
end: contents.length,
|
|
102
|
+
}, 0);
|
|
103
|
+
}
|
|
104
|
+
const decodingError = (message) => new DecodingError(message);
|
|
105
|
+
// ── Framing (X.690 §8.1, §10.1) ──────────────────────────────────────────────
|
|
106
|
+
function readElement(context, offset, limit) {
|
|
107
|
+
const { bytes, strict } = context;
|
|
108
|
+
if (offset >= limit) {
|
|
109
|
+
throw new DecodingError("DER: truncated, expected an element", offset);
|
|
110
|
+
}
|
|
111
|
+
const first = bytes[offset];
|
|
112
|
+
let position = offset + 1;
|
|
113
|
+
const tagClass = TAG_CLASSES[first >> 6];
|
|
114
|
+
const constructed = (first & 0x20) !== 0;
|
|
115
|
+
let tagNumber = first & 0x1f;
|
|
116
|
+
if (tagNumber === 0x1f) {
|
|
117
|
+
if (position < limit && bytes[position] === 0x80) {
|
|
118
|
+
throw new DecodingError("DER: tag number with a leading 0x80 octet (X.690 §8.1.2.4.2)", offset);
|
|
119
|
+
}
|
|
120
|
+
tagNumber = 0;
|
|
121
|
+
let octet;
|
|
122
|
+
do {
|
|
123
|
+
if (position >= limit) {
|
|
124
|
+
throw new DecodingError("DER: truncated tag number", offset);
|
|
125
|
+
}
|
|
126
|
+
if (tagNumber > MAX_TAG_PREFIX) {
|
|
127
|
+
throw new DecodingError("DER: tag number beyond Number.MAX_SAFE_INTEGER", offset);
|
|
128
|
+
}
|
|
129
|
+
octet = bytes[position];
|
|
130
|
+
position += 1;
|
|
131
|
+
tagNumber = tagNumber * 128 + (octet & 0x7f);
|
|
132
|
+
} while ((octet & 0x80) !== 0);
|
|
133
|
+
if (tagNumber < 0x1f) {
|
|
134
|
+
throw new DecodingError(`DER: tag number ${tagNumber} in the long form (X.690 §8.1.2.2)`, offset);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (tagClass === "universal" && tagNumber === 0) {
|
|
138
|
+
throw new DecodingError("DER: end-of-contents octets (universal tag 0) where a value is expected (X.690 §8.1.5, §10.1)", offset);
|
|
139
|
+
}
|
|
140
|
+
if (position >= limit) {
|
|
141
|
+
throw new DecodingError("DER: truncated, expected a length", offset);
|
|
142
|
+
}
|
|
143
|
+
const lengthStart = position;
|
|
144
|
+
const lengthOctet = bytes[position];
|
|
145
|
+
position += 1;
|
|
146
|
+
let length;
|
|
147
|
+
if (lengthOctet < 0x80) {
|
|
148
|
+
length = lengthOctet;
|
|
149
|
+
}
|
|
150
|
+
else if (lengthOctet === 0x80) {
|
|
151
|
+
throw new DecodingError("DER: indefinite length form (X.690 §10.1)", lengthStart);
|
|
152
|
+
}
|
|
153
|
+
else if (lengthOctet === 0xff) {
|
|
154
|
+
throw new DecodingError("DER: reserved length octet 0xFF (X.690 §8.1.3.5)", lengthStart);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
const count = lengthOctet & 0x7f;
|
|
158
|
+
if (count > limit - position) {
|
|
159
|
+
throw new DecodingError("DER: truncated length", lengthStart);
|
|
160
|
+
}
|
|
161
|
+
if (strict && bytes[position] === 0) {
|
|
162
|
+
throw new DecodingError("DER: length with a leading zero octet (X.690 §10.1)", lengthStart);
|
|
163
|
+
}
|
|
164
|
+
length = 0;
|
|
165
|
+
for (let index = 0; index < count; index++) {
|
|
166
|
+
length = length * 256 + bytes[position];
|
|
167
|
+
position += 1;
|
|
168
|
+
if (length > limit) {
|
|
169
|
+
throw new DecodingError("DER: length exceeds the input", lengthStart);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (strict && length < 0x80) {
|
|
173
|
+
throw new DecodingError(`DER: length ${length} in the long form (X.690 §10.1)`, lengthStart);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (length > limit - position) {
|
|
177
|
+
throw new DecodingError(`DER: length ${length} exceeds the ${limit - position} byte(s) available`, lengthStart);
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
tag: { tagClass, tagNumber, constructed },
|
|
181
|
+
start: offset,
|
|
182
|
+
contentStart: position,
|
|
183
|
+
end: position + length,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
/** A fresh copy: `slice` on a Buffer would be a view of the input. */
|
|
187
|
+
function copyBytes(view) {
|
|
188
|
+
return new Uint8Array(view);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Checks the framing of every TLV nested in `[start, end)` whose type the
|
|
192
|
+
* schema does not give (ANY, open types, unknown extensions): lengths and, in
|
|
193
|
+
* strict mode, their minimal forms, tag forms, no end-of-contents octets —
|
|
194
|
+
* recursing into constructed encodings. Type-specific contents rules cannot be
|
|
195
|
+
* checked without a schema. Iterative: the input cannot exhaust the stack.
|
|
196
|
+
*
|
|
197
|
+
* Complexity: O(n) in the bytes checked; memory O(depth).
|
|
198
|
+
*/
|
|
199
|
+
function checkOpaqueFraming(context, start, end) {
|
|
200
|
+
const ends = [end];
|
|
201
|
+
let position = start;
|
|
202
|
+
while (ends.length > 0) {
|
|
203
|
+
const limit = ends[ends.length - 1];
|
|
204
|
+
if (position >= limit) {
|
|
205
|
+
ends.pop();
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const element = readElement(context, position, limit);
|
|
209
|
+
if (element.tag.constructed) {
|
|
210
|
+
position = element.contentStart;
|
|
211
|
+
ends.push(element.end);
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
position = element.end;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/** The nested framing of a constructed element whose type is not known. */
|
|
219
|
+
function checkOpaqueContents(context, element) {
|
|
220
|
+
if (element.tag.constructed) {
|
|
221
|
+
checkOpaqueFraming(context, element.contentStart, element.end);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
// ── Values ───────────────────────────────────────────────────────────────────
|
|
225
|
+
function applyTransforms(value, transforms) {
|
|
226
|
+
let result = value;
|
|
227
|
+
for (let index = transforms.length - 1; index >= 0; index--) {
|
|
228
|
+
result = transforms[index](result);
|
|
229
|
+
}
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
232
|
+
/** Decodes `element` as a whole TLV of `def`, its tag included. */
|
|
233
|
+
function decodeValue(context, def, element, depth) {
|
|
234
|
+
if (depth > MAX_DEPTH) {
|
|
235
|
+
throw new DecodingError(`DER: nesting deeper than ${MAX_DEPTH} values`, element.start);
|
|
236
|
+
}
|
|
237
|
+
const transforms = [];
|
|
238
|
+
const base = unwrapDef(def, decodingError, transforms);
|
|
239
|
+
let value;
|
|
240
|
+
if (base.kind === "choice") {
|
|
241
|
+
value = decodeChoice(context, base, element, depth);
|
|
242
|
+
}
|
|
243
|
+
else if (base.kind === "any" || base.kind === "openType") {
|
|
244
|
+
checkOpaqueContents(context, element);
|
|
245
|
+
value = {
|
|
246
|
+
encoded: copyBytes(context.bytes.subarray(element.start, element.end)),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
else if (base.kind === "tagged") {
|
|
250
|
+
const { tag } = element;
|
|
251
|
+
if (tag.tagClass !== base.tag.tagClass ||
|
|
252
|
+
tag.tagNumber !== base.tag.tagNumber) {
|
|
253
|
+
throw new DecodingError(`DER: expected ${base.tag.tagClass} ${base.tag.tagNumber}, got ${tag.tagClass} ${tag.tagNumber}`, element.start);
|
|
254
|
+
}
|
|
255
|
+
value = decodeTaggedContents(context, base, element, depth);
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
const expected = universalTagFor(base);
|
|
259
|
+
if (expected === undefined) {
|
|
260
|
+
throw new DecodingError(`Cannot DER-decode def kind: ${base.kind}`, element.start);
|
|
261
|
+
}
|
|
262
|
+
const { tag } = element;
|
|
263
|
+
if (tag.tagClass !== "universal" || tag.tagNumber !== expected.tagNumber) {
|
|
264
|
+
throw new DecodingError(`DER: expected UNIVERSAL ${expected.tagNumber}, got ${tag.tagClass} ${tag.tagNumber}`, element.start);
|
|
265
|
+
}
|
|
266
|
+
checkConstructedBit(element, expected.constructed);
|
|
267
|
+
value = decodeUniversalContents(context, base, element, depth);
|
|
268
|
+
}
|
|
269
|
+
return applyTransforms(value, transforms);
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Decodes the contents of `element` as `def` whose own tag was replaced by an
|
|
273
|
+
* IMPLICIT tag already checked by the caller.
|
|
274
|
+
*/
|
|
275
|
+
function decodeContents(context, def, element, depth) {
|
|
276
|
+
if (depth > MAX_DEPTH) {
|
|
277
|
+
throw new DecodingError(`DER: nesting deeper than ${MAX_DEPTH} values`, element.start);
|
|
278
|
+
}
|
|
279
|
+
const transforms = [];
|
|
280
|
+
const base = unwrapDef(def, decodingError, transforms);
|
|
281
|
+
const value = base.kind === "tagged"
|
|
282
|
+
? decodeTaggedContents(context, base, element, depth)
|
|
283
|
+
: decodeUniversalContents(context, base, element, depth);
|
|
284
|
+
return applyTransforms(value, transforms);
|
|
285
|
+
}
|
|
286
|
+
function checkConstructedBit(element, constructed) {
|
|
287
|
+
if (element.tag.constructed !== constructed) {
|
|
288
|
+
throw new DecodingError(`DER: ${constructed ? "primitive" : "constructed"} encoding where the type requires the ${constructed ? "constructed" : "primitive"} form (X.690 §8.1.2.5, §10.2)`, element.start);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function decodeTaggedContents(context, def, element, depth) {
|
|
292
|
+
if (effectiveTaggingMode(def, decodingError) === "explicit") {
|
|
293
|
+
checkConstructedBit(element, true);
|
|
294
|
+
const inner = readElement(context, element.contentStart, element.end);
|
|
295
|
+
if (inner.end !== element.end) {
|
|
296
|
+
throw new DecodingError("DER: bytes after the value inside an EXPLICIT tag (X.690 §8.14)", inner.end);
|
|
297
|
+
}
|
|
298
|
+
return decodeValue(context, def.innerType, inner, depth + 1);
|
|
299
|
+
}
|
|
300
|
+
const constructed = isConstructedEncoding(def.innerType, decodingError);
|
|
301
|
+
if (constructed !== undefined) {
|
|
302
|
+
checkConstructedBit(element, constructed);
|
|
303
|
+
}
|
|
304
|
+
return decodeContents(context, def.innerType, element, depth + 1);
|
|
305
|
+
}
|
|
306
|
+
function decodeChoice(context, def, element, depth) {
|
|
307
|
+
let fallback;
|
|
308
|
+
for (const alternative of choiceAlternatives(def)) {
|
|
309
|
+
const tags = outerTagSet(alternative.type, decodingError);
|
|
310
|
+
if (tags === undefined) {
|
|
311
|
+
fallback ??= alternative;
|
|
312
|
+
}
|
|
313
|
+
else if (outerTagSetHas(tags, element.tag)) {
|
|
314
|
+
return {
|
|
315
|
+
kind: alternative.name,
|
|
316
|
+
value: decodeValue(context, alternative.type, element, depth + 1),
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (fallback !== undefined) {
|
|
321
|
+
return {
|
|
322
|
+
kind: fallback.name,
|
|
323
|
+
value: decodeValue(context, fallback.type, element, depth + 1),
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
throw new DecodingError(`DER: CHOICE has no alternative for ${element.tag.tagClass} ${element.tag.tagNumber}`, element.start);
|
|
327
|
+
}
|
|
328
|
+
function decodeUniversalContents(context, def, element, depth) {
|
|
329
|
+
const { strict } = context;
|
|
330
|
+
const contents = context.bytes.subarray(element.contentStart, element.end);
|
|
331
|
+
const at = element.contentStart;
|
|
46
332
|
switch (def.kind) {
|
|
47
333
|
case "boolean":
|
|
334
|
+
if (contents.length !== 1) {
|
|
335
|
+
throw new DecodingError("DER: BOOLEAN contents must be one octet (X.690 §8.2.1)", at);
|
|
336
|
+
}
|
|
48
337
|
if (strict && contents[0] !== 0x00 && contents[0] !== 0xff) {
|
|
49
|
-
throw new DecodingError("DER: BOOLEAN TRUE must be encoded as 0xFF");
|
|
338
|
+
throw new DecodingError("DER: BOOLEAN TRUE must be encoded as 0xFF (X.690 §11.1)", at);
|
|
50
339
|
}
|
|
51
340
|
return contents[0] !== 0x00;
|
|
52
341
|
case "integer":
|
|
53
342
|
case "enumerated":
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (contents.length === 0)
|
|
57
|
-
throw new DecodingError("DER: BIT STRING must have at least 1 content octet");
|
|
58
|
-
const unusedBits = contents[0];
|
|
59
|
-
// Trailing zero check only applies when unusedBits > 0 (X.690 §11.2)
|
|
60
|
-
// When unusedBits = 0, all bytes are meaningful data (e.g. RSA signature)
|
|
61
|
-
if (strict &&
|
|
62
|
-
unusedBits > 0 &&
|
|
63
|
-
contents.length > 1 &&
|
|
64
|
-
contents[contents.length - 1] === 0x00) {
|
|
65
|
-
throw new DecodingError("DER: BIT STRING has trailing zero bytes with unused bits > 0");
|
|
343
|
+
if (contents.length === 0) {
|
|
344
|
+
throw new DecodingError("DER: INTEGER contents must not be empty (X.690 §8.3.1)", at);
|
|
66
345
|
}
|
|
67
|
-
|
|
68
|
-
|
|
346
|
+
if (contents.length > 1 &&
|
|
347
|
+
((contents[0] === 0x00 && (contents[1] & 0x80) === 0) ||
|
|
348
|
+
(contents[0] === 0xff && (contents[1] & 0x80) !== 0))) {
|
|
349
|
+
throw new DecodingError("DER: INTEGER with a redundant leading octet (X.690 §8.3.2)", at);
|
|
350
|
+
}
|
|
351
|
+
// Linear: core parses long values from hex digits.
|
|
352
|
+
return bigIntFromBytesBe(contents);
|
|
353
|
+
case "bitString":
|
|
354
|
+
return decodeBitString(context, def.namedBits !== undefined && def.namedBits.length > 0, contents, at);
|
|
69
355
|
case "octetString":
|
|
70
|
-
return contents
|
|
356
|
+
return copyBytes(contents);
|
|
71
357
|
case "null":
|
|
358
|
+
if (contents.length !== 0) {
|
|
359
|
+
throw new DecodingError("DER: NULL contents must be empty (X.690 §8.8.2)", at);
|
|
360
|
+
}
|
|
72
361
|
return null;
|
|
73
362
|
case "objectIdentifier":
|
|
74
|
-
return decodeOid(contents);
|
|
363
|
+
return withOffset(at, () => decodeOid(contents));
|
|
75
364
|
case "relativeOid":
|
|
76
|
-
return decodeRelativeOid(contents);
|
|
365
|
+
return withOffset(at, () => decodeRelativeOid(contents));
|
|
77
366
|
case "oidIri":
|
|
78
367
|
case "relativeOidIri":
|
|
79
|
-
return decodeText(contents, "utf-8", { fatal: true });
|
|
368
|
+
return withOffset(at, () => decodeText(contents, "utf-8", { fatal: true, ignoreBOM: true }));
|
|
80
369
|
case "real":
|
|
81
|
-
return decodeReal(contents);
|
|
82
|
-
case "any":
|
|
83
|
-
return { encoded: contents.slice() };
|
|
370
|
+
return withOffset(at, () => decodeReal(contents));
|
|
84
371
|
case "utf8String":
|
|
85
|
-
|
|
372
|
+
// `ignoreBOM`: a leading U+FEFF is part of the value, not a mark.
|
|
373
|
+
return withOffset(at, () => decodeText(contents, "utf-8", { fatal: true, ignoreBOM: true }));
|
|
86
374
|
case "numericString":
|
|
87
375
|
case "printableString":
|
|
88
376
|
case "teletexString":
|
|
@@ -93,13 +381,15 @@ export function derDecodeContents(def, contents, strict) {
|
|
|
93
381
|
case "generalString":
|
|
94
382
|
return decodeText(contents, "latin1");
|
|
95
383
|
case "universalString":
|
|
96
|
-
|
|
384
|
+
checkCodeUnitLength(contents, 4, "UniversalString", at);
|
|
385
|
+
return withOffset(at, () => decodeText(contents, "utf-32be", { fatal: true }));
|
|
97
386
|
case "bmpString":
|
|
98
|
-
|
|
387
|
+
checkCodeUnitLength(contents, 2, "BMPString", at);
|
|
388
|
+
return withOffset(at, () => decodeText(contents, "utf-16be", { fatal: true, ignoreBOM: true }));
|
|
99
389
|
case "utcTime":
|
|
100
|
-
return decodeUtcTime(contents);
|
|
390
|
+
return withOffset(at, () => decodeUtcTime(contents, strict));
|
|
101
391
|
case "generalizedTime":
|
|
102
|
-
return decodeGeneralizedTime(contents);
|
|
392
|
+
return withOffset(at, () => decodeGeneralizedTime(contents, strict));
|
|
103
393
|
case "time":
|
|
104
394
|
case "date":
|
|
105
395
|
case "timeOfDay":
|
|
@@ -107,110 +397,262 @@ export function derDecodeContents(def, contents, strict) {
|
|
|
107
397
|
case "duration":
|
|
108
398
|
return decodeText(contents, "latin1");
|
|
109
399
|
case "sequence":
|
|
400
|
+
return decodeSequence(context, def, element, depth);
|
|
110
401
|
case "set":
|
|
111
|
-
return
|
|
402
|
+
return decodeSet(context, def, element, depth);
|
|
112
403
|
case "sequenceOf":
|
|
113
|
-
return decodeOfContents(def.elementType, contents, strict);
|
|
114
404
|
case "setOf":
|
|
115
|
-
return
|
|
405
|
+
return decodeElements(context, def.elementType, element, depth, strict && def.kind === "setOf");
|
|
116
406
|
case "external":
|
|
117
407
|
case "embeddedPdv":
|
|
118
408
|
case "characterString":
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
case "
|
|
123
|
-
|
|
409
|
+
checkOpaqueContents(context, element);
|
|
410
|
+
return copyBytes(contents);
|
|
411
|
+
case "any":
|
|
412
|
+
case "openType":
|
|
413
|
+
checkOpaqueContents(context, element);
|
|
414
|
+
return { encoded: copyBytes(contents) };
|
|
124
415
|
default:
|
|
125
|
-
throw new DecodingError(`Cannot DER-decode def kind: ${def.kind}
|
|
416
|
+
throw new DecodingError(`Cannot DER-decode def kind: ${def.kind}`, at);
|
|
126
417
|
}
|
|
127
418
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
throw new DecodingError(`DER: expected ${def.tag.tagClass} ${def.tag.tagNumber}, got ${tag.tagClass} ${tag.tagNumber}`, reader.position);
|
|
419
|
+
/** Re-throws a value decoder's error with the element's absolute offset. */
|
|
420
|
+
function withOffset(offset, decode) {
|
|
421
|
+
try {
|
|
422
|
+
return decode();
|
|
133
423
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return derDecodeReader(def.innerType, innerReader, strict);
|
|
424
|
+
catch (error) {
|
|
425
|
+
throw new DecodingError(error instanceof Error ? error.message : String(error), offset);
|
|
137
426
|
}
|
|
138
|
-
return derDecodeContents(def.innerType, contents, strict);
|
|
139
427
|
}
|
|
140
|
-
function
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
428
|
+
function checkCodeUnitLength(contents, unit, what, at) {
|
|
429
|
+
if (contents.length % unit !== 0) {
|
|
430
|
+
throw new DecodingError(`DER: ${what} length ${contents.length} is not a multiple of ${unit}`, at);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
function decodeBitString(context, hasNamedBits, contents, at) {
|
|
434
|
+
if (contents.length === 0) {
|
|
435
|
+
throw new DecodingError("DER: BIT STRING has no initial octet (X.690 §8.6.2)", at);
|
|
436
|
+
}
|
|
437
|
+
const unusedBits = contents[0];
|
|
438
|
+
if (unusedBits > 7) {
|
|
439
|
+
throw new DecodingError(`DER: BIT STRING with ${unusedBits} unused bits (X.690 §8.6.2.2)`, at);
|
|
440
|
+
}
|
|
441
|
+
if (contents.length === 1) {
|
|
442
|
+
if (unusedBits !== 0) {
|
|
443
|
+
throw new DecodingError("DER: empty BIT STRING with unused bits (X.690 §8.6.2.3)", at);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
else if (context.strict) {
|
|
447
|
+
const last = contents[contents.length - 1];
|
|
448
|
+
if ((last & ((1 << unusedBits) - 1)) !== 0) {
|
|
449
|
+
throw new DecodingError("DER: BIT STRING unused bits are not zero (X.690 §11.2.1)", at);
|
|
450
|
+
}
|
|
451
|
+
if (hasNamedBits && ((last >> unusedBits) & 1) === 0) {
|
|
452
|
+
throw new DecodingError("DER: BIT STRING with named bits ends in a zero bit (X.690 §11.2.2)", at);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
return { bytes: copyBytes(contents.subarray(1)), unusedBits };
|
|
456
|
+
}
|
|
457
|
+
// ── Constructed types ────────────────────────────────────────────────────────
|
|
458
|
+
function componentList(items) {
|
|
459
|
+
const components = [];
|
|
460
|
+
for (const item of items) {
|
|
461
|
+
if (item.kind === "component") {
|
|
462
|
+
components.push(item);
|
|
463
|
+
}
|
|
464
|
+
else if (item.kind === "extensionAdditionGroup") {
|
|
465
|
+
components.push(...item.components);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return components;
|
|
469
|
+
}
|
|
470
|
+
function isExtensible(def) {
|
|
471
|
+
return def.components.some((item) => item.kind === "extensionMarker");
|
|
472
|
+
}
|
|
473
|
+
/** Inferred defaults with no usable encoding can still supply a cloned output. */
|
|
474
|
+
function inferredDefaultEncoding(component) {
|
|
475
|
+
try {
|
|
476
|
+
return defaultEncoding(component);
|
|
477
|
+
}
|
|
478
|
+
catch (cause) {
|
|
479
|
+
if (cause instanceof SchemaDefinitionError)
|
|
480
|
+
throw cause;
|
|
481
|
+
return null;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Refuses, in strict mode, a component encoded although its value equals the
|
|
486
|
+
* DEFAULT (X.690 §11.5). The comparison is on DER encodings: the element has
|
|
487
|
+
* passed the strict checks, so it is canonical, and DER gives equal abstract
|
|
488
|
+
* values equal encodings — no rounding of the decoded value is involved.
|
|
489
|
+
*/
|
|
490
|
+
function checkNotDefault(context, component, element) {
|
|
491
|
+
if (component.defaultValue === undefined)
|
|
492
|
+
return;
|
|
493
|
+
const encoded = component.defaultWireValue !== undefined
|
|
494
|
+
? defaultEncoding(component)
|
|
495
|
+
: context.strict
|
|
496
|
+
? inferredDefaultEncoding(component)
|
|
497
|
+
: null;
|
|
498
|
+
if (!context.strict)
|
|
499
|
+
return;
|
|
500
|
+
if (encoded !== null &&
|
|
501
|
+
bytesIsEqual(context.bytes.subarray(element.start, element.end), encoded)) {
|
|
502
|
+
throw new DecodingError(`DER: component "${component.name}" is encoded with its DEFAULT value (X.690 §11.5)`, element.start);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Fills an absent component with a fresh copy of its declared DEFAULT, or
|
|
507
|
+
* nothing for an OPTIONAL one. A DEFAULT that also doubles as its wire value is
|
|
508
|
+
* decoded from the snapshot to fill in nested DEFAULTs. An output-only or
|
|
509
|
+
* separately supplied DEFAULT is cloned without replaying any transform.
|
|
510
|
+
*/
|
|
511
|
+
function setAbsent(component, result, at) {
|
|
512
|
+
if (component.defaultValue !== undefined) {
|
|
513
|
+
let encoded = null;
|
|
514
|
+
if (component.defaultWireValue !== undefined) {
|
|
515
|
+
defaultEncoding(component);
|
|
516
|
+
}
|
|
517
|
+
else {
|
|
518
|
+
encoded = inferredDefaultEncoding(component);
|
|
519
|
+
}
|
|
520
|
+
setRecordEntry(result, component.name, encoded === null
|
|
521
|
+
? freshDefault(component)
|
|
522
|
+
: derDecode(component.type, encoded));
|
|
523
|
+
}
|
|
524
|
+
else if (!component.optional) {
|
|
525
|
+
throw new DecodingError(`DER: required component "${component.name}" missing`, at);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
function decodeComponent(context, component, element, position, result, depth) {
|
|
529
|
+
if (position === element.end) {
|
|
530
|
+
setAbsent(component, result, position);
|
|
531
|
+
return position;
|
|
532
|
+
}
|
|
533
|
+
const child = readElement(context, position, element.end);
|
|
534
|
+
if (!outerTagSetHas(outerTagSet(component.type, decodingError), child.tag)) {
|
|
535
|
+
setAbsent(component, result, position);
|
|
536
|
+
return position;
|
|
537
|
+
}
|
|
538
|
+
const value = decodeValue(context, component.type, child, depth + 1);
|
|
539
|
+
checkNotDefault(context, component, child);
|
|
540
|
+
setRecordEntry(result, component.name, value);
|
|
541
|
+
return child.end;
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Skips extension additions this schema does not know, which a newer sender
|
|
545
|
+
* puts before the root components that follow the closing `...` (X.680 §25.4):
|
|
546
|
+
* every element whose tag none of those components can start with. Their
|
|
547
|
+
* framing is still checked. When one of them is an untagged ANY, nothing can be
|
|
548
|
+
* told apart and nothing is skipped.
|
|
549
|
+
*/
|
|
550
|
+
function skipUnknownAdditions(context, trailing, position, end) {
|
|
551
|
+
const sets = trailing.map((component) => outerTagSet(component.type, decodingError));
|
|
552
|
+
if (sets.some((tags) => tags === undefined)) {
|
|
553
|
+
return position;
|
|
554
|
+
}
|
|
555
|
+
let next = position;
|
|
556
|
+
while (next < end) {
|
|
557
|
+
const child = readElement(context, next, end);
|
|
558
|
+
if (sets.some((tags) => outerTagSetHas(tags, child.tag))) {
|
|
559
|
+
break;
|
|
154
560
|
}
|
|
561
|
+
checkOpaqueFraming(context, child.start, child.end);
|
|
562
|
+
next = child.end;
|
|
155
563
|
}
|
|
156
|
-
|
|
564
|
+
return next;
|
|
157
565
|
}
|
|
158
|
-
function
|
|
159
|
-
const reader = new ByteReader(contents);
|
|
566
|
+
function decodeSequence(context, def, element, depth) {
|
|
160
567
|
const result = {};
|
|
161
|
-
|
|
162
|
-
|
|
568
|
+
const items = def.components;
|
|
569
|
+
let position = element.contentStart;
|
|
570
|
+
let markers = 0;
|
|
571
|
+
for (let index = 0; index < items.length; index++) {
|
|
572
|
+
const item = items[index];
|
|
573
|
+
if (item.kind === "extensionMarker") {
|
|
574
|
+
markers++;
|
|
575
|
+
if (markers === 2) {
|
|
576
|
+
position = skipUnknownAdditions(context, componentList(items.slice(index + 1)), position, element.end);
|
|
577
|
+
}
|
|
163
578
|
continue;
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
decodeComponentFrom(reader, inner, result, strict);
|
|
579
|
+
}
|
|
580
|
+
if (item.kind === "componentsOf")
|
|
167
581
|
continue;
|
|
582
|
+
const members = item.kind === "component" ? [item] : item.components;
|
|
583
|
+
for (const component of members) {
|
|
584
|
+
position = decodeComponent(context, component, element, position, result, depth);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
if (position < element.end) {
|
|
588
|
+
if (markers !== 1) {
|
|
589
|
+
throw new DecodingError("DER: unexpected element after the last component", position);
|
|
168
590
|
}
|
|
169
|
-
|
|
591
|
+
// Unknown extension additions at the end: framing checked, then dropped.
|
|
592
|
+
checkOpaqueFraming(context, position, element.end);
|
|
170
593
|
}
|
|
171
594
|
return result;
|
|
172
595
|
}
|
|
173
|
-
function
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
596
|
+
function decodeSet(context, def, element, depth) {
|
|
597
|
+
const components = componentList(def.components);
|
|
598
|
+
const tagSets = components.map((component) => outerTagSet(component.type, decodingError));
|
|
599
|
+
const present = new Set();
|
|
600
|
+
const result = {};
|
|
601
|
+
const extensible = isExtensible(def);
|
|
602
|
+
let position = element.contentStart;
|
|
603
|
+
let previousKey;
|
|
604
|
+
while (position < element.end) {
|
|
605
|
+
const child = readElement(context, position, element.end);
|
|
606
|
+
const key = tagSortKey(child.tag);
|
|
607
|
+
if (context.strict && previousKey !== undefined && key < previousKey) {
|
|
608
|
+
throw new DecodingError("DER: SET components not in ascending tag order (X.690 §10.3)", child.start);
|
|
609
|
+
}
|
|
610
|
+
previousKey = key;
|
|
611
|
+
let index = tagSets.findIndex((tags) => tags !== undefined && outerTagSetHas(tags, child.tag));
|
|
612
|
+
if (index < 0) {
|
|
613
|
+
index = tagSets.findIndex((tags, candidate) => tags === undefined && !present.has(components[candidate]));
|
|
614
|
+
}
|
|
615
|
+
if (index < 0) {
|
|
616
|
+
if (!extensible) {
|
|
617
|
+
throw new DecodingError(`DER: SET has no component for ${child.tag.tagClass} ${child.tag.tagNumber}`, child.start);
|
|
618
|
+
}
|
|
619
|
+
checkOpaqueFraming(context, child.start, child.end);
|
|
620
|
+
}
|
|
621
|
+
else {
|
|
622
|
+
const component = components[index];
|
|
623
|
+
if (present.has(component)) {
|
|
624
|
+
throw new DecodingError(`DER: SET component "${component.name}" appears twice`, child.start);
|
|
625
|
+
}
|
|
626
|
+
present.add(component);
|
|
627
|
+
const value = decodeValue(context, component.type, child, depth + 1);
|
|
628
|
+
checkNotDefault(context, component, child);
|
|
629
|
+
setRecordEntry(result, component.name, value);
|
|
179
630
|
}
|
|
180
|
-
|
|
631
|
+
position = child.end;
|
|
181
632
|
}
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
const expectedTag = resolveOuterTag(comp.type);
|
|
186
|
-
if (expectedTag && !tagsMatch(expectedTag, nextTag)) {
|
|
187
|
-
if (comp.optional || comp.defaultValue !== undefined) {
|
|
188
|
-
if (comp.defaultValue !== undefined)
|
|
189
|
-
setRecordEntry(result, comp.name, comp.defaultValue);
|
|
190
|
-
return;
|
|
633
|
+
for (const component of components) {
|
|
634
|
+
if (!present.has(component)) {
|
|
635
|
+
setAbsent(component, result, element.end);
|
|
191
636
|
}
|
|
192
|
-
throw new DecodingError(`DER: required component "${comp.name}" has wrong tag`);
|
|
193
637
|
}
|
|
194
|
-
|
|
638
|
+
return result;
|
|
195
639
|
}
|
|
196
|
-
function
|
|
197
|
-
const reader = new ByteReader(contents);
|
|
640
|
+
function decodeElements(context, elementDef, element, depth, checkOrder) {
|
|
198
641
|
const result = [];
|
|
199
|
-
|
|
200
|
-
|
|
642
|
+
let position = element.contentStart;
|
|
643
|
+
let previous;
|
|
644
|
+
while (position < element.end) {
|
|
645
|
+
const child = readElement(context, position, element.end);
|
|
646
|
+
if (checkOrder) {
|
|
647
|
+
const encoding = context.bytes.subarray(child.start, child.end);
|
|
648
|
+
if (previous !== undefined && compareBytes(previous, encoding) > 0) {
|
|
649
|
+
throw new DecodingError("DER: SET OF elements not in ascending order (X.690 §11.6)", child.start);
|
|
650
|
+
}
|
|
651
|
+
previous = encoding;
|
|
652
|
+
}
|
|
653
|
+
result.push(decodeValue(context, elementDef, child, depth + 1));
|
|
654
|
+
position = child.end;
|
|
655
|
+
}
|
|
201
656
|
return result;
|
|
202
657
|
}
|
|
203
|
-
function resolveOuterTag(def) {
|
|
204
|
-
if (def.kind === "tagged")
|
|
205
|
-
return { tagClass: def.tag.tagClass, tagNumber: def.tag.tagNumber };
|
|
206
|
-
if (def.kind === "lazy")
|
|
207
|
-
return resolveOuterTag(def.getter());
|
|
208
|
-
const info = universalTagFor(def);
|
|
209
|
-
return info
|
|
210
|
-
? { tagClass: "universal", tagNumber: info.tagNumber }
|
|
211
|
-
: undefined;
|
|
212
|
-
}
|
|
213
|
-
function tagsMatch(a, b) {
|
|
214
|
-
return a.tagClass === b.tagClass && a.tagNumber === b.tagNumber;
|
|
215
|
-
}
|
|
216
658
|
//# sourceMappingURL=decode.js.map
|