@ac-kit/format-asn1 0.3.0 → 0.4.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 +81 -0
- package/README.md +132 -4
- package/dist/_encoding/constants.d.ts +2 -0
- package/dist/_encoding/constants.d.ts.map +1 -1
- package/dist/_encoding/constants.js +7 -0
- package/dist/_encoding/constants.js.map +1 -1
- package/dist/_encoding/decode-primitive.d.ts +39 -0
- package/dist/_encoding/decode-primitive.d.ts.map +1 -0
- package/dist/_encoding/decode-primitive.js +144 -0
- package/dist/_encoding/decode-primitive.js.map +1 -0
- package/dist/_encoding/def-tags.d.ts +36 -3
- package/dist/_encoding/def-tags.d.ts.map +1 -1
- package/dist/_encoding/def-tags.js +88 -3
- package/dist/_encoding/def-tags.js.map +1 -1
- package/dist/_encoding/encoded-tag-sort-key.d.ts +7 -0
- package/dist/_encoding/encoded-tag-sort-key.d.ts.map +1 -0
- package/dist/_encoding/encoded-tag-sort-key.js +13 -0
- package/dist/_encoding/encoded-tag-sort-key.js.map +1 -0
- package/dist/_encoding/tag.d.ts +1 -6
- package/dist/_encoding/tag.d.ts.map +1 -1
- package/dist/_encoding/tag.js +5 -43
- package/dist/_encoding/tag.js.map +1 -1
- package/dist/_encoding/tlv.d.ts +4 -25
- package/dist/_encoding/tlv.d.ts.map +1 -1
- package/dist/_encoding/tlv.js +12 -48
- package/dist/_encoding/tlv.js.map +1 -1
- package/dist/_encoding/values.d.ts.map +1 -1
- package/dist/_encoding/values.js +31 -19
- package/dist/_encoding/values.js.map +1 -1
- package/dist/_encoding/vlq.d.ts.map +1 -1
- package/dist/_encoding/vlq.js +2 -2
- package/dist/_encoding/vlq.js.map +1 -1
- package/dist/_encoding/with-offset.d.ts +12 -0
- package/dist/_encoding/with-offset.d.ts.map +1 -0
- package/dist/_encoding/with-offset.js +22 -0
- package/dist/_encoding/with-offset.js.map +1 -0
- package/dist/ber/_decode.d.ts +25 -0
- package/dist/ber/_decode.d.ts.map +1 -0
- package/dist/ber/_decode.js +409 -0
- package/dist/ber/_decode.js.map +1 -0
- package/dist/ber/codec.d.ts +3 -2
- package/dist/ber/codec.d.ts.map +1 -1
- package/dist/ber/codec.js +45 -16
- package/dist/ber/codec.js.map +1 -1
- package/dist/ber/decode.d.ts +30 -1
- package/dist/ber/decode.d.ts.map +1 -1
- package/dist/ber/decode.js +31 -221
- package/dist/ber/decode.js.map +1 -1
- package/dist/ber/encode.d.ts +7 -1
- package/dist/ber/encode.d.ts.map +1 -1
- package/dist/ber/encode.js +46 -53
- package/dist/ber/encode.js.map +1 -1
- package/dist/cer/decode.d.ts +20 -1
- package/dist/cer/decode.d.ts.map +1 -1
- package/dist/cer/decode.js +27 -221
- package/dist/cer/decode.js.map +1 -1
- package/dist/cer/encode.d.ts +22 -6
- package/dist/cer/encode.d.ts.map +1 -1
- package/dist/cer/encode.js +147 -136
- package/dist/cer/encode.js.map +1 -1
- package/dist/der/decode.d.ts +27 -21
- package/dist/der/decode.d.ts.map +1 -1
- package/dist/der/decode.js +323 -398
- package/dist/der/decode.js.map +1 -1
- package/dist/der/encode.js +5 -8
- package/dist/der/encode.js.map +1 -1
- package/dist/errors.d.ts +56 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +31 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/per/decode.d.ts +9 -0
- package/dist/per/decode.d.ts.map +1 -1
- package/dist/per/decode.js +114 -32
- package/dist/per/decode.js.map +1 -1
- package/dist/per/encode.js +1 -1
- package/dist/per/encode.js.map +1 -1
- package/dist/reader/_framing.d.ts +38 -0
- package/dist/reader/_framing.d.ts.map +1 -0
- package/dist/reader/_framing.js +144 -0
- package/dist/reader/_framing.js.map +1 -0
- package/dist/reader/_header.d.ts +49 -0
- package/dist/reader/_header.d.ts.map +1 -0
- package/dist/reader/_header.js +130 -0
- package/dist/reader/_header.js.map +1 -0
- package/dist/reader/ber-reader.d.ts +240 -0
- package/dist/reader/ber-reader.d.ts.map +1 -0
- package/dist/reader/ber-reader.js +567 -0
- package/dist/reader/ber-reader.js.map +1 -0
- package/dist/reader/inspect.d.ts +68 -0
- package/dist/reader/inspect.d.ts.map +1 -0
- package/dist/reader/inspect.js +104 -0
- package/dist/reader/inspect.js.map +1 -0
- package/dist/schema/universal-tag-name.d.ts +26 -0
- package/dist/schema/universal-tag-name.d.ts.map +1 -0
- package/dist/schema/universal-tag-name.js +74 -0
- package/dist/schema/universal-tag-name.js.map +1 -0
- package/package.json +9 -8
- package/dist/_encoding/errors.d.ts +0 -8
- package/dist/_encoding/errors.d.ts.map +0 -1
- package/dist/_encoding/errors.js +0 -15
- package/dist/_encoding/errors.js.map +0 -1
package/dist/der/decode.js
CHANGED
|
@@ -1,51 +1,52 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { bytesIsEqual, compareBytes, setRecordEntry } from "@ac-kit/core";
|
|
2
|
+
import { MAX_DECODE_DEPTH } from "../_encoding/constants.js";
|
|
3
|
+
import { copyBytes, decodePrimitiveContents, } from "../_encoding/decode-primitive.js";
|
|
4
|
+
import { choiceAlternatives, componentList, effectiveTaggingMode, findSetComponent, isConstructedEncoding, isExtensible, isStringKind, outerTagSet, outerTagSetHas, unwrapDef, } from "../_encoding/def-tags.js";
|
|
3
5
|
import { freshDefault } from "../_encoding/default-output.js";
|
|
4
|
-
import { DecodingError } from "../_encoding/errors.js";
|
|
5
6
|
import { tagSortKey } from "../_encoding/tlv.js";
|
|
6
|
-
import { decodeGeneralizedTime, decodeOid, decodeReal, decodeRelativeOid, decodeUtcTime, } from "../_encoding/values.js";
|
|
7
7
|
import { universalTagFor } from "../ber.js";
|
|
8
|
+
import { DecodingError } from "../errors.js";
|
|
9
|
+
import { checkDefiniteFraming, createShared, RULES_BER, RULES_DER, } from "../reader/_framing.js";
|
|
10
|
+
import { DEFAULT_MAX_TAG_OCTETS } from "../reader/_header.js";
|
|
11
|
+
import { BerReader } from "../reader/ber-reader.js";
|
|
8
12
|
import { SchemaDefinitionError } from "../schema/errors.js";
|
|
9
13
|
import { defaultEncoding } from "./default-encoding.js";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"application",
|
|
21
|
-
"context",
|
|
22
|
-
"private",
|
|
23
|
-
];
|
|
14
|
+
const DER_STRICT = {
|
|
15
|
+
label: "DER",
|
|
16
|
+
canonical: true,
|
|
17
|
+
strictBoolean: true,
|
|
18
|
+
};
|
|
19
|
+
const DER_LENIENT = {
|
|
20
|
+
label: "DER",
|
|
21
|
+
canonical: false,
|
|
22
|
+
strictBoolean: false,
|
|
23
|
+
};
|
|
24
24
|
/**
|
|
25
25
|
* DER decoder (X.690 §10, §11): decodes exactly one value of `def` from
|
|
26
26
|
* `bytes`.
|
|
27
27
|
*
|
|
28
|
-
*
|
|
29
|
-
* length form (
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* match the type (§8.1.2.5, §10.2: DER strings are primitive, EXPLICIT
|
|
33
|
-
* constructed); bytes left over inside an EXPLICIT tag or at the end of a
|
|
28
|
+
* The framing is `BerReader`'s with the rules `"der"`, or with `"ber"` minus
|
|
29
|
+
* the indefinite length form when not strict (see its documentation for the
|
|
30
|
+
* identifier, length and end-of-contents rules, which every rule set applies).
|
|
31
|
+
* Always refused, as malformed BER as well: a primitive/constructed bit that
|
|
32
|
+
* does not match the type (§8.1.2.5, §10.2: DER strings are primitive, EXPLICIT
|
|
33
|
+
* tags constructed); bytes left over inside an EXPLICIT tag or at the end of a
|
|
34
34
|
* non-extensible SEQUENCE or SET; a BOOLEAN that is not one octet; an empty
|
|
35
|
-
* INTEGER or ENUMERATED
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* does not exist; a SET with
|
|
35
|
+
* INTEGER or ENUMERATED, or one with a redundant leading octet (§8.3.2); a BIT
|
|
36
|
+
* STRING without its initial octet, with more than 7 unused bits or empty with
|
|
37
|
+
* unused bits; a non-empty NULL; a BMPString or UniversalString whose length is
|
|
38
|
+
* not a multiple of 2 or 4; a UTCTime or GeneralizedTime that is not
|
|
39
|
+
* syntactically valid or names a date or time that does not exist; a SET with
|
|
40
|
+
* an unknown, duplicate or missing component; the indefinite length form
|
|
41
|
+
* (§10.1; use `berDecode` for BER input).
|
|
40
42
|
*
|
|
41
43
|
* Refused in strict mode (the default) only, being canonical-form rules:
|
|
42
44
|
* trailing bytes after the value; a length not in the fewest octets (§10.1); a
|
|
43
|
-
* BOOLEAN other than `0x00`/`0xFF` (§11.1);
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* than `YYMMDDHHMMSSZ` (§11.8); a GeneralizedTime other than
|
|
45
|
+
* BOOLEAN other than `0x00`/`0xFF` (§11.1); BIT STRING unused bits not zero
|
|
46
|
+
* (§11.2.1) or, for a type with named bits, trailing zero bits (§11.2.2); a
|
|
47
|
+
* component encoded although equal to its known wire DEFAULT (§11.5); SET
|
|
48
|
+
* components out of tag order (§10.3); SET OF elements out of order (§11.6); a
|
|
49
|
+
* UTCTime other than `YYMMDDHHMMSSZ` (§11.8); a GeneralizedTime other than
|
|
49
50
|
* `YYYYMMDDHHMMSS[.f]Z` with no trailing zero in the fraction (§11.7).
|
|
50
51
|
*
|
|
51
52
|
* An absent OPTIONAL or DEFAULT component is recognised by one tag of lookahead
|
|
@@ -64,18 +65,30 @@ const TAG_CLASSES = [
|
|
|
64
65
|
* @param bytes - One DER encoding.
|
|
65
66
|
* @param strict - `false` relaxes the canonical-form rules listed above, for
|
|
66
67
|
* input from encoders that are BER-valid but not DER-exact.
|
|
67
|
-
* @returns The decoded value. ANY values keep their original bytes
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
68
|
+
* @returns The decoded value. ANY values keep their original bytes; a tag
|
|
69
|
+
* number above `Number.MAX_SAFE_INTEGER` (up to 16 identifier octets after
|
|
70
|
+
* the first) is accepted inside them.
|
|
71
|
+
* @throws {DecodingError} On every violation listed above, with the offset of
|
|
72
|
+
* the offending element and a `code` (`truncated`, `overrun`, `invalid-tag`,
|
|
73
|
+
* `invalid-length`, `invalid-end-of-contents`, `unexpected-element`,
|
|
74
|
+
* `missing-element`, `trailing-data`, `invalid-contents`, `non-canonical`,
|
|
75
|
+
* `limit-exceeded`), or if an absent output-only DEFAULT cannot be
|
|
76
|
+
* structured-cloned.
|
|
71
77
|
*/
|
|
72
78
|
export function derDecode(def, bytes, strict = true) {
|
|
73
|
-
const context = {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
79
|
+
const context = {
|
|
80
|
+
bytes,
|
|
81
|
+
strict,
|
|
82
|
+
primitive: strict ? DER_STRICT : DER_LENIENT,
|
|
83
|
+
framing: undefined,
|
|
84
|
+
};
|
|
85
|
+
const reader = new BerReader(bytes, strict ? "der" : "ber");
|
|
86
|
+
peekDer(context, reader);
|
|
87
|
+
const end = reader.contentsOffset + reader.contentsLength;
|
|
88
|
+
if (strict && end !== bytes.length) {
|
|
89
|
+
throw new DecodingError(`DER: ${bytes.length - end} byte(s) after the value`, end, "trailing-data");
|
|
77
90
|
}
|
|
78
|
-
return decodeValue(context, def,
|
|
91
|
+
return decodeValue(context, def, reader, 0);
|
|
79
92
|
}
|
|
80
93
|
/**
|
|
81
94
|
* Decodes DER contents octets — the value part of a TLV whose identifier and
|
|
@@ -92,134 +105,47 @@ export function derDecodeContents(def, contents, strict) {
|
|
|
92
105
|
const context = {
|
|
93
106
|
bytes: contents,
|
|
94
107
|
strict,
|
|
108
|
+
primitive: strict ? DER_STRICT : DER_LENIENT,
|
|
109
|
+
framing: undefined,
|
|
95
110
|
};
|
|
96
|
-
|
|
97
|
-
return decodeContents(context, def, {
|
|
98
|
-
tag: { tagClass: "universal", tagNumber: 0, constructed },
|
|
99
|
-
start: 0,
|
|
100
|
-
contentStart: 0,
|
|
101
|
-
end: contents.length,
|
|
102
|
-
}, 0);
|
|
111
|
+
return decodeLevelAs(context, def, new BerReader(contents, strict ? "der" : "ber"), contents, 0);
|
|
103
112
|
}
|
|
104
|
-
const decodingError = (message) => new DecodingError(message);
|
|
113
|
+
const decodingError = (message) => new DecodingError(message, 0, "unexpected-element");
|
|
105
114
|
// ── Framing (X.690 §8.1, §10.1) ──────────────────────────────────────────────
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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);
|
|
115
|
+
/**
|
|
116
|
+
* Reads the next header, refusing the indefinite form also when the reader is
|
|
117
|
+
* not enforcing DER, and reports a failed length rule with the DER wording.
|
|
118
|
+
*/
|
|
119
|
+
function peekDer(context, reader) {
|
|
120
|
+
reader.peekHeader();
|
|
121
|
+
if (!context.strict && reader.contentsLength === null) {
|
|
122
|
+
throw new DecodingError("DER: indefinite length form (X.690 §10.1)", reader.contentsOffset - 1, "non-canonical");
|
|
178
123
|
}
|
|
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
124
|
}
|
|
190
125
|
/**
|
|
191
|
-
* Checks the framing of every
|
|
192
|
-
* schema does not give (ANY, open types, unknown
|
|
193
|
-
* strict mode, their minimal forms, tag forms, no
|
|
194
|
-
*
|
|
195
|
-
* checked without a schema. Iterative: the input cannot exhaust the
|
|
126
|
+
* Checks the framing of every element in `[from, end)` of the input, nested
|
|
127
|
+
* ones included, whose type the schema does not give (ANY, open types, unknown
|
|
128
|
+
* extensions): lengths and, in strict mode, their minimal forms, tag forms, no
|
|
129
|
+
* end-of-contents octets, no indefinite length. Type-specific contents rules
|
|
130
|
+
* cannot be checked without a schema. Iterative: the input cannot exhaust the
|
|
131
|
+
* stack.
|
|
196
132
|
*
|
|
197
133
|
* Complexity: O(n) in the bytes checked; memory O(depth).
|
|
198
134
|
*/
|
|
199
|
-
function
|
|
200
|
-
|
|
201
|
-
|
|
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
|
-
}
|
|
135
|
+
function checkRange(context, from, end) {
|
|
136
|
+
context.framing ??= createShared(context.bytes, context.strict ? RULES_DER : RULES_BER, DEFAULT_MAX_TAG_OCTETS, undefined);
|
|
137
|
+
checkDefiniteFraming(context.framing, from, end);
|
|
217
138
|
}
|
|
218
|
-
/**
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
139
|
+
/**
|
|
140
|
+
* Consumes the element whose header is current, checking the framing of every
|
|
141
|
+
* element nested in it (see {@link checkRange}).
|
|
142
|
+
*/
|
|
143
|
+
function checkFramingDeep(context, reader) {
|
|
144
|
+
if (reader.constructed) {
|
|
145
|
+
const from = reader.contentsOffset;
|
|
146
|
+
checkRange(context, from, from + reader.contentsLength);
|
|
222
147
|
}
|
|
148
|
+
reader.skip();
|
|
223
149
|
}
|
|
224
150
|
// ── Values ───────────────────────────────────────────────────────────────────
|
|
225
151
|
function applyTransforms(value, transforms) {
|
|
@@ -229,246 +155,238 @@ function applyTransforms(value, transforms) {
|
|
|
229
155
|
}
|
|
230
156
|
return result;
|
|
231
157
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
158
|
+
function isWrapper(def) {
|
|
159
|
+
return (def.kind === "lazy" ||
|
|
160
|
+
def.kind === "transform" ||
|
|
161
|
+
def.kind === "parameterizedTypeInstance");
|
|
162
|
+
}
|
|
163
|
+
function checkDepth(reader, depth) {
|
|
164
|
+
if (depth > MAX_DECODE_DEPTH) {
|
|
165
|
+
throw new DecodingError(`DER: nesting deeper than ${MAX_DECODE_DEPTH} values`, reader.offset, "limit-exceeded");
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function describeTag(reader) {
|
|
169
|
+
return `${reader.tagClass} ${reader.tagNumber ?? reader.bigTagNumber()}`;
|
|
170
|
+
}
|
|
171
|
+
/** Decodes the element whose header is current as a whole TLV of `def`. */
|
|
172
|
+
function decodeValue(context, def, reader, depth) {
|
|
173
|
+
checkDepth(reader, depth);
|
|
174
|
+
let transforms;
|
|
175
|
+
let base = def;
|
|
176
|
+
if (isWrapper(def)) {
|
|
177
|
+
transforms = [];
|
|
178
|
+
base = unwrapDef(def, decodingError, transforms);
|
|
236
179
|
}
|
|
237
|
-
const transforms = [];
|
|
238
|
-
const base = unwrapDef(def, decodingError, transforms);
|
|
239
180
|
let value;
|
|
240
181
|
if (base.kind === "choice") {
|
|
241
|
-
value = decodeChoice(context, base,
|
|
182
|
+
value = decodeChoice(context, base, reader, depth);
|
|
242
183
|
}
|
|
243
184
|
else if (base.kind === "any" || base.kind === "openType") {
|
|
244
|
-
|
|
185
|
+
const start = reader.offset;
|
|
186
|
+
checkFramingDeep(context, reader);
|
|
245
187
|
value = {
|
|
246
|
-
encoded: copyBytes(context.bytes.subarray(
|
|
188
|
+
encoded: copyBytes(context.bytes.subarray(start, reader.offset)),
|
|
247
189
|
};
|
|
248
190
|
}
|
|
249
191
|
else if (base.kind === "tagged") {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
tag.
|
|
253
|
-
throw new DecodingError(`DER: expected ${base.tag.tagClass} ${base.tag.tagNumber}, got ${tag.tagClass} ${tag.tagNumber}`, element.start);
|
|
192
|
+
if (reader.tagClass !== base.tag.tagClass ||
|
|
193
|
+
reader.tagNumber !== base.tag.tagNumber) {
|
|
194
|
+
throw new DecodingError(`DER: expected ${base.tag.tagClass} ${base.tag.tagNumber}, got ${describeTag(reader)}`, reader.offset, "unexpected-element");
|
|
254
195
|
}
|
|
255
|
-
value = decodeTaggedContents(context, base,
|
|
196
|
+
value = decodeTaggedContents(context, base, reader, depth);
|
|
256
197
|
}
|
|
257
198
|
else {
|
|
258
199
|
const expected = universalTagFor(base);
|
|
259
200
|
if (expected === undefined) {
|
|
260
|
-
throw new DecodingError(`Cannot DER-decode def kind: ${base.kind}`, element
|
|
201
|
+
throw new DecodingError(`Cannot DER-decode def kind: ${base.kind}`, reader.offset, "unexpected-element");
|
|
261
202
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
throw new DecodingError(`DER: expected UNIVERSAL ${expected.tagNumber}, got ${
|
|
203
|
+
if (reader.tagClass !== "universal" ||
|
|
204
|
+
reader.tagNumber !== expected.tagNumber) {
|
|
205
|
+
throw new DecodingError(`DER: expected UNIVERSAL ${expected.tagNumber}, got ${describeTag(reader)}`, reader.offset, "unexpected-element");
|
|
265
206
|
}
|
|
266
|
-
checkConstructedBit(
|
|
267
|
-
value = decodeUniversalContents(context, base,
|
|
207
|
+
checkConstructedBit(reader, expected.constructed, base);
|
|
208
|
+
value = decodeUniversalContents(context, base, reader, depth);
|
|
268
209
|
}
|
|
269
|
-
return applyTransforms(value, transforms);
|
|
210
|
+
return transforms === undefined ? value : applyTransforms(value, transforms);
|
|
270
211
|
}
|
|
271
212
|
/**
|
|
272
|
-
* Decodes the contents of
|
|
273
|
-
* IMPLICIT tag already checked by the caller.
|
|
213
|
+
* Decodes the contents of the element whose header is current as `def` whose
|
|
214
|
+
* own tag was replaced by an IMPLICIT tag already checked by the caller.
|
|
274
215
|
*/
|
|
275
|
-
function decodeContents(context, def,
|
|
276
|
-
|
|
277
|
-
|
|
216
|
+
function decodeContents(context, def, reader, depth) {
|
|
217
|
+
checkDepth(reader, depth);
|
|
218
|
+
let transforms;
|
|
219
|
+
let base = def;
|
|
220
|
+
if (isWrapper(def)) {
|
|
221
|
+
transforms = [];
|
|
222
|
+
base = unwrapDef(def, decodingError, transforms);
|
|
278
223
|
}
|
|
279
|
-
const transforms = [];
|
|
280
|
-
const base = unwrapDef(def, decodingError, transforms);
|
|
281
224
|
const value = base.kind === "tagged"
|
|
282
|
-
? decodeTaggedContents(context, base,
|
|
283
|
-
: decodeUniversalContents(context, base,
|
|
284
|
-
return applyTransforms(value, transforms);
|
|
225
|
+
? decodeTaggedContents(context, base, reader, depth)
|
|
226
|
+
: decodeUniversalContents(context, base, reader, depth);
|
|
227
|
+
return transforms === undefined ? value : applyTransforms(value, transforms);
|
|
285
228
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
229
|
+
/** The kind of the type underneath wrappers and tags. */
|
|
230
|
+
function leafKind(def) {
|
|
231
|
+
let current = unwrapDef(def, decodingError);
|
|
232
|
+
while (current.kind === "tagged") {
|
|
233
|
+
current = unwrapDef(current.innerType, decodingError);
|
|
289
234
|
}
|
|
235
|
+
return current.kind;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Refuses an element whose constructed bit is not the one the type needs (X.690
|
|
239
|
+
* §8.1.2.5, §10.2). A string sent constructed is valid BER, so it is
|
|
240
|
+
* non-canonical; any other mismatch is not an element of this type.
|
|
241
|
+
*/
|
|
242
|
+
function checkConstructedBit(reader, constructed, def) {
|
|
243
|
+
if (reader.constructed === constructed)
|
|
244
|
+
return;
|
|
245
|
+
const kind = leafKind(def);
|
|
246
|
+
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)`, reader.offset, !constructed && (isStringKind(kind) || kind === "bitString")
|
|
247
|
+
? "non-canonical"
|
|
248
|
+
: "unexpected-element");
|
|
290
249
|
}
|
|
291
|
-
function decodeTaggedContents(context, def,
|
|
250
|
+
function decodeTaggedContents(context, def, reader, depth) {
|
|
292
251
|
if (effectiveTaggingMode(def, decodingError) === "explicit") {
|
|
293
|
-
checkConstructedBit(
|
|
294
|
-
const inner =
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
return decodeValue(context, def.innerType, inner, depth + 1);
|
|
252
|
+
checkConstructedBit(reader, true, def);
|
|
253
|
+
const inner = reader.enter();
|
|
254
|
+
const value = decodeExplicitContents(context, def, inner, depth);
|
|
255
|
+
inner.finish();
|
|
256
|
+
return value;
|
|
299
257
|
}
|
|
300
258
|
const constructed = isConstructedEncoding(def.innerType, decodingError);
|
|
301
259
|
if (constructed !== undefined) {
|
|
302
|
-
checkConstructedBit(
|
|
260
|
+
checkConstructedBit(reader, constructed, def.innerType);
|
|
261
|
+
}
|
|
262
|
+
return decodeContents(context, def.innerType, reader, depth + 1);
|
|
263
|
+
}
|
|
264
|
+
/** The one value inside an EXPLICIT tag; `inner` is left at its end. */
|
|
265
|
+
function decodeExplicitContents(context, def, inner, depth) {
|
|
266
|
+
if (inner.atEnd) {
|
|
267
|
+
throw new DecodingError("DER: EXPLICIT tag with no value inside (X.690 §8.14)", inner.offset, "missing-element");
|
|
303
268
|
}
|
|
304
|
-
|
|
269
|
+
peekDer(context, inner);
|
|
270
|
+
const value = decodeValue(context, def.innerType, inner, depth + 1);
|
|
271
|
+
if (!inner.atEnd) {
|
|
272
|
+
throw new DecodingError("DER: bytes after the value inside an EXPLICIT tag (X.690 §8.14)", inner.offset, "trailing-data");
|
|
273
|
+
}
|
|
274
|
+
return value;
|
|
305
275
|
}
|
|
306
|
-
function decodeChoice(context, def,
|
|
276
|
+
function decodeChoice(context, def, reader, depth) {
|
|
277
|
+
const tagClass = reader.tagClass;
|
|
278
|
+
const tagNumber = reader.tagNumber;
|
|
307
279
|
let fallback;
|
|
308
280
|
for (const alternative of choiceAlternatives(def)) {
|
|
309
281
|
const tags = outerTagSet(alternative.type, decodingError);
|
|
310
282
|
if (tags === undefined) {
|
|
311
283
|
fallback ??= alternative;
|
|
312
284
|
}
|
|
313
|
-
else if (outerTagSetHas(tags,
|
|
285
|
+
else if (outerTagSetHas(tags, tagClass, tagNumber)) {
|
|
314
286
|
return {
|
|
315
287
|
kind: alternative.name,
|
|
316
|
-
value: decodeValue(context, alternative.type,
|
|
288
|
+
value: decodeValue(context, alternative.type, reader, depth + 1),
|
|
317
289
|
};
|
|
318
290
|
}
|
|
319
291
|
}
|
|
320
292
|
if (fallback !== undefined) {
|
|
321
293
|
return {
|
|
322
294
|
kind: fallback.name,
|
|
323
|
-
value: decodeValue(context, fallback.type,
|
|
295
|
+
value: decodeValue(context, fallback.type, reader, depth + 1),
|
|
324
296
|
};
|
|
325
297
|
}
|
|
326
|
-
throw new DecodingError(`DER: CHOICE has no alternative for ${
|
|
298
|
+
throw new DecodingError(`DER: CHOICE has no alternative for ${describeTag(reader)}`, reader.offset, "unexpected-element");
|
|
327
299
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
const contents = context.bytes.subarray(element.contentStart, element.end);
|
|
331
|
-
const at = element.contentStart;
|
|
300
|
+
/** Decodes the element whose header is current as the universal type `def`. */
|
|
301
|
+
function decodeUniversalContents(context, def, reader, depth) {
|
|
332
302
|
switch (def.kind) {
|
|
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
|
-
}
|
|
337
|
-
if (strict && contents[0] !== 0x00 && contents[0] !== 0xff) {
|
|
338
|
-
throw new DecodingError("DER: BOOLEAN TRUE must be encoded as 0xFF (X.690 §11.1)", at);
|
|
339
|
-
}
|
|
340
|
-
return contents[0] !== 0x00;
|
|
341
|
-
case "integer":
|
|
342
|
-
case "enumerated":
|
|
343
|
-
if (contents.length === 0) {
|
|
344
|
-
throw new DecodingError("DER: INTEGER contents must not be empty (X.690 §8.3.1)", at);
|
|
345
|
-
}
|
|
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);
|
|
355
|
-
case "octetString":
|
|
356
|
-
return copyBytes(contents);
|
|
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
|
-
}
|
|
361
|
-
return null;
|
|
362
|
-
case "objectIdentifier":
|
|
363
|
-
return withOffset(at, () => decodeOid(contents));
|
|
364
|
-
case "relativeOid":
|
|
365
|
-
return withOffset(at, () => decodeRelativeOid(contents));
|
|
366
|
-
case "oidIri":
|
|
367
|
-
case "relativeOidIri":
|
|
368
|
-
return withOffset(at, () => decodeText(contents, "utf-8", { fatal: true, ignoreBOM: true }));
|
|
369
|
-
case "real":
|
|
370
|
-
return withOffset(at, () => decodeReal(contents));
|
|
371
|
-
case "utf8String":
|
|
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 }));
|
|
374
|
-
case "numericString":
|
|
375
|
-
case "printableString":
|
|
376
|
-
case "teletexString":
|
|
377
|
-
case "videotexString":
|
|
378
|
-
case "ia5String":
|
|
379
|
-
case "graphicString":
|
|
380
|
-
case "visibleString":
|
|
381
|
-
case "generalString":
|
|
382
|
-
return decodeText(contents, "latin1");
|
|
383
|
-
case "universalString":
|
|
384
|
-
checkCodeUnitLength(contents, 4, "UniversalString", at);
|
|
385
|
-
return withOffset(at, () => decodeText(contents, "utf-32be", { fatal: true }));
|
|
386
|
-
case "bmpString":
|
|
387
|
-
checkCodeUnitLength(contents, 2, "BMPString", at);
|
|
388
|
-
return withOffset(at, () => decodeText(contents, "utf-16be", { fatal: true, ignoreBOM: true }));
|
|
389
|
-
case "utcTime":
|
|
390
|
-
return withOffset(at, () => decodeUtcTime(contents, strict));
|
|
391
|
-
case "generalizedTime":
|
|
392
|
-
return withOffset(at, () => decodeGeneralizedTime(contents, strict));
|
|
393
|
-
case "time":
|
|
394
|
-
case "date":
|
|
395
|
-
case "timeOfDay":
|
|
396
|
-
case "dateTime":
|
|
397
|
-
case "duration":
|
|
398
|
-
return decodeText(contents, "latin1");
|
|
399
303
|
case "sequence":
|
|
400
|
-
return decodeSequence(context, def, element, depth);
|
|
401
304
|
case "set":
|
|
402
|
-
return decodeSet(context, def, element, depth);
|
|
403
305
|
case "sequenceOf":
|
|
404
|
-
case "setOf":
|
|
405
|
-
|
|
306
|
+
case "setOf": {
|
|
307
|
+
const children = reader.enter();
|
|
308
|
+
const value = decodeConstructedContents(context, def, children, depth);
|
|
309
|
+
children.finish();
|
|
310
|
+
return value;
|
|
311
|
+
}
|
|
406
312
|
case "external":
|
|
407
313
|
case "embeddedPdv":
|
|
408
314
|
case "characterString":
|
|
409
|
-
checkOpaqueContents(context, element);
|
|
410
|
-
return copyBytes(contents);
|
|
411
315
|
case "any":
|
|
412
|
-
case "openType":
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
}
|
|
427
|
-
}
|
|
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);
|
|
316
|
+
case "openType": {
|
|
317
|
+
// Everything the schema does not give: framing checked, bytes kept.
|
|
318
|
+
const start = reader.contentsOffset;
|
|
319
|
+
const end = start + reader.contentsLength;
|
|
320
|
+
checkFramingDeep(context, reader);
|
|
321
|
+
const contents = copyBytes(context.bytes.subarray(start, end));
|
|
322
|
+
return def.kind === "any" || def.kind === "openType"
|
|
323
|
+
? { encoded: contents }
|
|
324
|
+
: contents;
|
|
325
|
+
}
|
|
326
|
+
default: {
|
|
327
|
+
const at = reader.contentsOffset;
|
|
328
|
+
return decodePrimitiveContents(def, reader.readContents(), at, context.primitive);
|
|
329
|
+
}
|
|
431
330
|
}
|
|
432
331
|
}
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
332
|
+
/**
|
|
333
|
+
* Decodes `contents`, the contents octets of an element of type `def`, which
|
|
334
|
+
* `level` reads: the children of a constructed type, nothing for a primitive
|
|
335
|
+
* one. The reader's offsets are `contents`' own.
|
|
336
|
+
*/
|
|
337
|
+
function decodeLevelAs(context, def, level, contents, depth) {
|
|
338
|
+
checkDepth(level, depth);
|
|
339
|
+
const transforms = [];
|
|
340
|
+
const base = unwrapDef(def, decodingError, transforms);
|
|
341
|
+
let value;
|
|
342
|
+
if (base.kind === "tagged") {
|
|
343
|
+
if (effectiveTaggingMode(base, decodingError) === "explicit") {
|
|
344
|
+
value = decodeExplicitContents(context, base, level, depth);
|
|
345
|
+
level.finish();
|
|
444
346
|
}
|
|
445
|
-
|
|
446
|
-
|
|
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);
|
|
347
|
+
else {
|
|
348
|
+
value = decodeLevelAs(context, base.innerType, level, contents, depth + 1);
|
|
450
349
|
}
|
|
451
|
-
|
|
452
|
-
|
|
350
|
+
}
|
|
351
|
+
else if (base.kind === "any" || base.kind === "openType") {
|
|
352
|
+
value = { encoded: copyBytes(contents) };
|
|
353
|
+
}
|
|
354
|
+
else if (isConstructedEncoding(base, decodingError) ?? false) {
|
|
355
|
+
switch (base.kind) {
|
|
356
|
+
case "sequence":
|
|
357
|
+
case "set":
|
|
358
|
+
case "sequenceOf":
|
|
359
|
+
case "setOf":
|
|
360
|
+
value = decodeConstructedContents(context, base, level, depth);
|
|
361
|
+
level.finish();
|
|
362
|
+
break;
|
|
363
|
+
default:
|
|
364
|
+
checkRange(context, 0, contents.length);
|
|
365
|
+
value = copyBytes(contents);
|
|
453
366
|
}
|
|
454
367
|
}
|
|
455
|
-
|
|
368
|
+
else {
|
|
369
|
+
value = decodePrimitiveContents(base, contents, 0, context.primitive);
|
|
370
|
+
}
|
|
371
|
+
return applyTransforms(value, transforms);
|
|
456
372
|
}
|
|
457
373
|
// ── Constructed types ────────────────────────────────────────────────────────
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
374
|
+
/**
|
|
375
|
+
* The children of a SEQUENCE, SET, SEQUENCE OF or SET OF; `children` is left at
|
|
376
|
+
* its end.
|
|
377
|
+
*/
|
|
378
|
+
function decodeConstructedContents(context, def, children, depth) {
|
|
379
|
+
switch (def.kind) {
|
|
380
|
+
case "sequence":
|
|
381
|
+
return decodeSequence(context, def, children, depth);
|
|
382
|
+
case "set":
|
|
383
|
+
return decodeSet(context, def, children, depth);
|
|
384
|
+
case "sequenceOf":
|
|
385
|
+
case "setOf":
|
|
386
|
+
return decodeElements(context, def.elementType, children, depth, context.strict && def.kind === "setOf");
|
|
387
|
+
default:
|
|
388
|
+
throw new DecodingError(`Cannot DER-decode def kind: ${def.kind}`, children.offset, "unexpected-element");
|
|
467
389
|
}
|
|
468
|
-
return components;
|
|
469
|
-
}
|
|
470
|
-
function isExtensible(def) {
|
|
471
|
-
return def.components.some((item) => item.kind === "extensionMarker");
|
|
472
390
|
}
|
|
473
391
|
/** Inferred defaults with no usable encoding can still supply a cloned output. */
|
|
474
392
|
function inferredDefaultEncoding(component) {
|
|
@@ -486,8 +404,11 @@ function inferredDefaultEncoding(component) {
|
|
|
486
404
|
* DEFAULT (X.690 §11.5). The comparison is on DER encodings: the element has
|
|
487
405
|
* passed the strict checks, so it is canonical, and DER gives equal abstract
|
|
488
406
|
* values equal encodings — no rounding of the decoded value is involved.
|
|
407
|
+
*
|
|
408
|
+
* @param start - Offset of the component's element.
|
|
409
|
+
* @param end - Offset just after it.
|
|
489
410
|
*/
|
|
490
|
-
function checkNotDefault(context, component,
|
|
411
|
+
function checkNotDefault(context, component, start, end) {
|
|
491
412
|
if (component.defaultValue === undefined)
|
|
492
413
|
return;
|
|
493
414
|
const encoded = component.defaultWireValue !== undefined
|
|
@@ -498,8 +419,8 @@ function checkNotDefault(context, component, element) {
|
|
|
498
419
|
if (!context.strict)
|
|
499
420
|
return;
|
|
500
421
|
if (encoded !== null &&
|
|
501
|
-
bytesIsEqual(context.bytes.subarray(
|
|
502
|
-
throw new DecodingError(`DER: component "${component.name}" is encoded with its DEFAULT value (X.690 §11.5)`,
|
|
422
|
+
bytesIsEqual(context.bytes.subarray(start, end), encoded)) {
|
|
423
|
+
throw new DecodingError(`DER: component "${component.name}" is encoded with its DEFAULT value (X.690 §11.5)`, start, "non-canonical");
|
|
503
424
|
}
|
|
504
425
|
}
|
|
505
426
|
/**
|
|
@@ -522,23 +443,23 @@ function setAbsent(component, result, at) {
|
|
|
522
443
|
: derDecode(component.type, encoded));
|
|
523
444
|
}
|
|
524
445
|
else if (!component.optional) {
|
|
525
|
-
throw new DecodingError(`DER: required component "${component.name}" missing`, at);
|
|
446
|
+
throw new DecodingError(`DER: required component "${component.name}" missing`, at, "missing-element");
|
|
526
447
|
}
|
|
527
448
|
}
|
|
528
|
-
function decodeComponent(context, component,
|
|
529
|
-
if (
|
|
530
|
-
setAbsent(component, result,
|
|
531
|
-
return
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
if (!outerTagSetHas(outerTagSet(component.type, decodingError),
|
|
535
|
-
setAbsent(component, result,
|
|
536
|
-
return
|
|
537
|
-
}
|
|
538
|
-
const
|
|
539
|
-
|
|
449
|
+
function decodeComponent(context, component, children, result, depth) {
|
|
450
|
+
if (children.atEnd) {
|
|
451
|
+
setAbsent(component, result, children.offset);
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
peekDer(context, children);
|
|
455
|
+
if (!outerTagSetHas(outerTagSet(component.type, decodingError), children.tagClass, children.tagNumber)) {
|
|
456
|
+
setAbsent(component, result, children.offset);
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
const start = children.offset;
|
|
460
|
+
const value = decodeValue(context, component.type, children, depth + 1);
|
|
461
|
+
checkNotDefault(context, component, start, children.offset);
|
|
540
462
|
setRecordEntry(result, component.name, value);
|
|
541
|
-
return child.end;
|
|
542
463
|
}
|
|
543
464
|
/**
|
|
544
465
|
* Skips extension additions this schema does not know, which a newer sender
|
|
@@ -547,111 +468,115 @@ function decodeComponent(context, component, element, position, result, depth) {
|
|
|
547
468
|
* framing is still checked. When one of them is an untagged ANY, nothing can be
|
|
548
469
|
* told apart and nothing is skipped.
|
|
549
470
|
*/
|
|
550
|
-
function skipUnknownAdditions(context, trailing,
|
|
471
|
+
function skipUnknownAdditions(context, trailing, children) {
|
|
551
472
|
const sets = trailing.map((component) => outerTagSet(component.type, decodingError));
|
|
552
473
|
if (sets.some((tags) => tags === undefined)) {
|
|
553
|
-
return
|
|
474
|
+
return;
|
|
554
475
|
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
const
|
|
558
|
-
|
|
476
|
+
while (!children.atEnd) {
|
|
477
|
+
peekDer(context, children);
|
|
478
|
+
const tagClass = children.tagClass;
|
|
479
|
+
const tagNumber = children.tagNumber;
|
|
480
|
+
if (sets.some((tags) => outerTagSetHas(tags, tagClass, tagNumber))) {
|
|
559
481
|
break;
|
|
560
482
|
}
|
|
561
|
-
|
|
562
|
-
next = child.end;
|
|
483
|
+
checkFramingDeep(context, children);
|
|
563
484
|
}
|
|
564
|
-
return next;
|
|
565
485
|
}
|
|
566
|
-
function decodeSequence(context, def,
|
|
486
|
+
function decodeSequence(context, def, children, depth) {
|
|
567
487
|
const result = {};
|
|
568
488
|
const items = def.components;
|
|
569
|
-
let position = element.contentStart;
|
|
570
489
|
let markers = 0;
|
|
571
490
|
for (let index = 0; index < items.length; index++) {
|
|
572
491
|
const item = items[index];
|
|
573
492
|
if (item.kind === "extensionMarker") {
|
|
574
493
|
markers++;
|
|
575
494
|
if (markers === 2) {
|
|
576
|
-
|
|
495
|
+
skipUnknownAdditions(context, componentList(items.slice(index + 1)), children);
|
|
577
496
|
}
|
|
578
497
|
continue;
|
|
579
498
|
}
|
|
580
499
|
if (item.kind === "componentsOf")
|
|
581
500
|
continue;
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
501
|
+
if (item.kind === "component") {
|
|
502
|
+
decodeComponent(context, item, children, result, depth);
|
|
503
|
+
}
|
|
504
|
+
else {
|
|
505
|
+
for (const component of item.components) {
|
|
506
|
+
decodeComponent(context, component, children, result, depth);
|
|
507
|
+
}
|
|
585
508
|
}
|
|
586
509
|
}
|
|
587
|
-
if (
|
|
510
|
+
if (!children.atEnd) {
|
|
588
511
|
if (markers !== 1) {
|
|
589
|
-
throw new DecodingError("DER: unexpected element after the last component",
|
|
512
|
+
throw new DecodingError("DER: unexpected element after the last component", children.offset, "trailing-data");
|
|
590
513
|
}
|
|
591
514
|
// Unknown extension additions at the end: framing checked, then dropped.
|
|
592
|
-
|
|
515
|
+
while (!children.atEnd) {
|
|
516
|
+
peekDer(context, children);
|
|
517
|
+
checkFramingDeep(context, children);
|
|
518
|
+
}
|
|
593
519
|
}
|
|
594
520
|
return result;
|
|
595
521
|
}
|
|
596
|
-
function decodeSet(context, def,
|
|
522
|
+
function decodeSet(context, def, children, depth) {
|
|
597
523
|
const components = componentList(def.components);
|
|
598
524
|
const tagSets = components.map((component) => outerTagSet(component.type, decodingError));
|
|
599
525
|
const present = new Set();
|
|
600
526
|
const result = {};
|
|
601
527
|
const extensible = isExtensible(def);
|
|
602
|
-
let position = element.contentStart;
|
|
603
528
|
let previousKey;
|
|
604
|
-
while (
|
|
605
|
-
|
|
606
|
-
const
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
529
|
+
while (!children.atEnd) {
|
|
530
|
+
peekDer(context, children);
|
|
531
|
+
const tagClass = children.tagClass;
|
|
532
|
+
const tagNumber = children.tagNumber;
|
|
533
|
+
const start = children.offset;
|
|
534
|
+
if (context.strict) {
|
|
535
|
+
const key = tagSortKey(tagClass, tagNumber ?? children.bigTagNumber());
|
|
536
|
+
if (previousKey !== undefined && key < previousKey) {
|
|
537
|
+
throw new DecodingError("DER: SET components not in ascending tag order (X.690 §10.3)", start, "non-canonical");
|
|
538
|
+
}
|
|
539
|
+
previousKey = key;
|
|
614
540
|
}
|
|
541
|
+
const index = findSetComponent(components, tagSets, present, tagClass, tagNumber);
|
|
615
542
|
if (index < 0) {
|
|
616
543
|
if (!extensible) {
|
|
617
|
-
throw new DecodingError(`DER: SET has no component for ${
|
|
544
|
+
throw new DecodingError(`DER: SET has no component for ${describeTag(children)}`, start, "unexpected-element");
|
|
618
545
|
}
|
|
619
|
-
|
|
546
|
+
checkFramingDeep(context, children);
|
|
620
547
|
}
|
|
621
548
|
else {
|
|
622
549
|
const component = components[index];
|
|
623
550
|
if (present.has(component)) {
|
|
624
|
-
throw new DecodingError(`DER: SET component "${component.name}" appears twice`,
|
|
551
|
+
throw new DecodingError(`DER: SET component "${component.name}" appears twice`, start, "unexpected-element");
|
|
625
552
|
}
|
|
626
553
|
present.add(component);
|
|
627
|
-
const value = decodeValue(context, component.type,
|
|
628
|
-
checkNotDefault(context, component,
|
|
554
|
+
const value = decodeValue(context, component.type, children, depth + 1);
|
|
555
|
+
checkNotDefault(context, component, start, children.offset);
|
|
629
556
|
setRecordEntry(result, component.name, value);
|
|
630
557
|
}
|
|
631
|
-
position = child.end;
|
|
632
558
|
}
|
|
633
559
|
for (const component of components) {
|
|
634
560
|
if (!present.has(component)) {
|
|
635
|
-
setAbsent(component, result,
|
|
561
|
+
setAbsent(component, result, children.offset);
|
|
636
562
|
}
|
|
637
563
|
}
|
|
638
564
|
return result;
|
|
639
565
|
}
|
|
640
|
-
function decodeElements(context, elementDef,
|
|
566
|
+
function decodeElements(context, elementDef, children, depth, checkOrder) {
|
|
641
567
|
const result = [];
|
|
642
|
-
let position = element.contentStart;
|
|
643
568
|
let previous;
|
|
644
|
-
while (
|
|
645
|
-
|
|
569
|
+
while (!children.atEnd) {
|
|
570
|
+
peekDer(context, children);
|
|
646
571
|
if (checkOrder) {
|
|
647
|
-
const
|
|
572
|
+
const start = children.offset;
|
|
573
|
+
const encoding = context.bytes.subarray(start, children.contentsOffset + children.contentsLength);
|
|
648
574
|
if (previous !== undefined && compareBytes(previous, encoding) > 0) {
|
|
649
|
-
throw new DecodingError("DER: SET OF elements not in ascending order (X.690 §11.6)",
|
|
575
|
+
throw new DecodingError("DER: SET OF elements not in ascending order (X.690 §11.6)", start, "non-canonical");
|
|
650
576
|
}
|
|
651
577
|
previous = encoding;
|
|
652
578
|
}
|
|
653
|
-
result.push(decodeValue(context, elementDef,
|
|
654
|
-
position = child.end;
|
|
579
|
+
result.push(decodeValue(context, elementDef, children, depth + 1));
|
|
655
580
|
}
|
|
656
581
|
return result;
|
|
657
582
|
}
|