@ac-kit/format-asn1 0.3.1 → 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 +68 -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
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { DecodingError } from "../errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* Runs a value decoder over contents starting at `offset`: a DecodingError it
|
|
4
|
+
* throws is re-based (`offset + error.offset`, same code); any other error
|
|
5
|
+
* becomes `invalid-contents` at `offset`, with it as `cause`.
|
|
6
|
+
*
|
|
7
|
+
* Wrap only calls whose errors carry contents-relative offsets (the `values.ts`
|
|
8
|
+
* decoders, `decodeText`); never a function that already throws with an
|
|
9
|
+
* absolute offset, or the offset would be added twice.
|
|
10
|
+
*/
|
|
11
|
+
export function withOffset(offset, decode) {
|
|
12
|
+
try {
|
|
13
|
+
return decode();
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
if (error instanceof DecodingError) {
|
|
17
|
+
throw new DecodingError(error.message, offset + error.offset, error.code, { cause: error });
|
|
18
|
+
}
|
|
19
|
+
throw new DecodingError(error instanceof Error ? error.message : String(error), offset, "invalid-contents", { cause: error });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=with-offset.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with-offset.js","sourceRoot":"","sources":["../../src/_encoding/with-offset.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;GAQG;AACH,MAAM,UAAU,UAAU,CAAI,MAAc,EAAE,MAAyB;IACtE,IAAI,CAAC;QACJ,OAAO,MAAM,EAAE,CAAC;IACjB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,IAAI,KAAK,YAAY,aAAa,EAAE,CAAC;YACpC,MAAM,IAAI,aAAa,CACtB,KAAK,CAAC,OAAO,EACb,MAAM,GAAG,KAAK,CAAC,MAAM,EACrB,KAAK,CAAC,IAAI,EACV,EAAE,KAAK,EAAE,KAAK,EAAE,CAChB,CAAC;QACH,CAAC;QACD,MAAM,IAAI,aAAa,CACtB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EACtD,MAAM,EACN,kBAAkB,EAClB,EAAE,KAAK,EAAE,KAAK,EAAE,CAChB,CAAC;IACH,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Callable } from "@ac-kit/core";
|
|
2
|
+
import { AnyAsn1TypeDef } from "../schema/types/any-def.js";
|
|
3
|
+
import type { Asn1ComponentDef } from "../schema/types/constructed/component.js";
|
|
4
|
+
/** What differs between the BER and the CER decoder. */
|
|
5
|
+
export type BerFamilyPolicy = {
|
|
6
|
+
readonly rules: "ber" | "cer";
|
|
7
|
+
/** `berDecode`'s `strict`: BOOLEAN TRUE must be 0xFF. */
|
|
8
|
+
readonly strictBoolean: boolean;
|
|
9
|
+
/** `cerDecode`'s check of every component, called as the component is met. */
|
|
10
|
+
readonly checkComponent: Callable<[component: Asn1ComponentDef]> | undefined;
|
|
11
|
+
};
|
|
12
|
+
export declare const BER_POLICY: BerFamilyPolicy;
|
|
13
|
+
export declare const BER_STRICT_POLICY: BerFamilyPolicy;
|
|
14
|
+
/**
|
|
15
|
+
* Decodes the first element of `bytes` as `def`, under the BER or CER rules
|
|
16
|
+
* `policy` names; bytes after it are not read.
|
|
17
|
+
*
|
|
18
|
+
* @returns The value and `end`, the offset just after its element.
|
|
19
|
+
* @throws {DecodingError} For anything wrong with the input.
|
|
20
|
+
*/
|
|
21
|
+
export declare function decodeBerFamily(def: AnyAsn1TypeDef, bytes: Uint8Array, policy: BerFamilyPolicy): {
|
|
22
|
+
value: unknown;
|
|
23
|
+
end: number;
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=_decode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_decode.d.ts","sourceRoot":"","sources":["../../src/ber/_decode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAuB7D,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAG5D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAKjF,wDAAwD;AACxD,MAAM,MAAM,eAAe,GAAG;IAC7B,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAAC;IAC9B,yDAAyD;IACzD,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;IAChC,8EAA8E;IAC9E,QAAQ,CAAC,cAAc,EAAE,QAAQ,CAAC,CAAC,SAAS,EAAE,gBAAgB,CAAC,CAAC,GAAG,SAAS,CAAC;CAC7E,CAAC;AAEF,eAAO,MAAM,UAAU,EAAE,eAIxB,CAAC;AAEF,eAAO,MAAM,iBAAiB,EAAE,eAI/B,CAAC;AAeF;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC9B,GAAG,EAAE,cAAc,EACnB,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,eAAe,GACrB;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAejC"}
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
import { setRecordEntry } from "@ac-kit/core";
|
|
2
|
+
import { MAX_DECODE_DEPTH } from "../_encoding/constants.js";
|
|
3
|
+
import { decodePrimitiveContents, } from "../_encoding/decode-primitive.js";
|
|
4
|
+
import { choiceAlternatives, componentList, effectiveTaggingMode, findSetComponent, isExtensible, isStringKind, outerTagSet, outerTagSetHas, smallestTagSortKey, unwrapDef, } from "../_encoding/def-tags.js";
|
|
5
|
+
import { freshDefault } from "../_encoding/default-output.js";
|
|
6
|
+
import { tagSortKey } from "../_encoding/tlv.js";
|
|
7
|
+
import { DecodingError } from "../errors.js";
|
|
8
|
+
import { BerReader } from "../reader/ber-reader.js";
|
|
9
|
+
import { universalTagFor } from "./tags.js";
|
|
10
|
+
export const BER_POLICY = {
|
|
11
|
+
rules: "ber",
|
|
12
|
+
strictBoolean: false,
|
|
13
|
+
checkComponent: undefined,
|
|
14
|
+
};
|
|
15
|
+
export const BER_STRICT_POLICY = {
|
|
16
|
+
rules: "ber",
|
|
17
|
+
strictBoolean: true,
|
|
18
|
+
checkComponent: undefined,
|
|
19
|
+
};
|
|
20
|
+
const decodingError = (message) => new DecodingError(message, 0, "unexpected-element");
|
|
21
|
+
/**
|
|
22
|
+
* Decodes the first element of `bytes` as `def`, under the BER or CER rules
|
|
23
|
+
* `policy` names; bytes after it are not read.
|
|
24
|
+
*
|
|
25
|
+
* @returns The value and `end`, the offset just after its element.
|
|
26
|
+
* @throws {DecodingError} For anything wrong with the input.
|
|
27
|
+
*/
|
|
28
|
+
export function decodeBerFamily(def, bytes, policy) {
|
|
29
|
+
const reader = new BerReader(bytes, policy.rules);
|
|
30
|
+
const label = policy.rules === "cer" ? "CER" : "BER";
|
|
31
|
+
const context = {
|
|
32
|
+
policy,
|
|
33
|
+
bytes,
|
|
34
|
+
label,
|
|
35
|
+
primitive: {
|
|
36
|
+
label,
|
|
37
|
+
canonical: false,
|
|
38
|
+
strictBoolean: policy.strictBoolean,
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
const value = decodeValue(context, def, reader);
|
|
42
|
+
return { value, end: reader.offset };
|
|
43
|
+
}
|
|
44
|
+
/** Decodes the next element of `reader` as a whole TLV of `def`. */
|
|
45
|
+
function decodeValue(context, def, reader) {
|
|
46
|
+
reader.peekHeader();
|
|
47
|
+
const transforms = [];
|
|
48
|
+
const base = unwrapDef(def, decodingError, transforms);
|
|
49
|
+
let value;
|
|
50
|
+
if (base.kind === "choice") {
|
|
51
|
+
value = decodeChoice(context, base, reader);
|
|
52
|
+
}
|
|
53
|
+
else if (base.kind === "any" || base.kind === "openType") {
|
|
54
|
+
value = { encoded: new Uint8Array(reader.readElement()) };
|
|
55
|
+
}
|
|
56
|
+
else if (base.kind === "tagged") {
|
|
57
|
+
if (reader.tagClass !== base.tag.tagClass ||
|
|
58
|
+
reader.tagNumber !== base.tag.tagNumber) {
|
|
59
|
+
throw new DecodingError(`${context.label}: expected ${base.tag.tagClass} ${base.tag.tagNumber}, got ${describeTag(reader)}`, reader.offset, "unexpected-element");
|
|
60
|
+
}
|
|
61
|
+
value = decodeTaggedContents(context, base, reader);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
const expected = universalTagFor(base);
|
|
65
|
+
if (expected === undefined) {
|
|
66
|
+
throw new DecodingError(`Cannot ${context.label}-decode def kind: ${base.kind}`, reader.offset, "unexpected-element");
|
|
67
|
+
}
|
|
68
|
+
if (reader.tagClass !== "universal" ||
|
|
69
|
+
reader.tagNumber !== expected.tagNumber) {
|
|
70
|
+
throw new DecodingError(`${context.label}: expected UNIVERSAL ${expected.tagNumber}, got ${describeTag(reader)}`, reader.offset, "unexpected-element");
|
|
71
|
+
}
|
|
72
|
+
value = decodeContents(context, base, reader);
|
|
73
|
+
}
|
|
74
|
+
for (let index = transforms.length - 1; index >= 0; index--) {
|
|
75
|
+
value = transforms[index](value);
|
|
76
|
+
}
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
function describeTag(reader) {
|
|
80
|
+
return `${reader.tagClass} ${reader.tagNumber ?? reader.bigTagNumber()}`;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Decodes the contents of the next element of `reader` as `def`, whose own tag
|
|
84
|
+
* was checked, or replaced by an IMPLICIT tag.
|
|
85
|
+
*/
|
|
86
|
+
function decodeContents(context, def, reader) {
|
|
87
|
+
const transforms = [];
|
|
88
|
+
const base = unwrapDef(def, decodingError, transforms);
|
|
89
|
+
let value = base.kind === "tagged"
|
|
90
|
+
? decodeTaggedContents(context, base, reader)
|
|
91
|
+
: decodeUniversalContents(context, base, reader);
|
|
92
|
+
for (let index = transforms.length - 1; index >= 0; index--) {
|
|
93
|
+
value = transforms[index](value);
|
|
94
|
+
}
|
|
95
|
+
return value;
|
|
96
|
+
}
|
|
97
|
+
function requireConstructed(context, reader) {
|
|
98
|
+
if (!reader.constructed) {
|
|
99
|
+
throw new DecodingError(`${context.label}: primitive encoding where the type requires the constructed form`, reader.offset, "unexpected-element");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function requirePrimitive(context, reader) {
|
|
103
|
+
if (reader.constructed) {
|
|
104
|
+
throw new DecodingError(`${context.label}: constructed encoding where the type requires the primitive form`, reader.offset, "unexpected-element");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** Enters the constructed element whose header is current. */
|
|
108
|
+
function enter(context, reader) {
|
|
109
|
+
const children = reader.enter();
|
|
110
|
+
if (children.depth > MAX_DECODE_DEPTH) {
|
|
111
|
+
throw new DecodingError(`${context.label}: nesting deeper than ${MAX_DECODE_DEPTH} values`, children.offset, "limit-exceeded");
|
|
112
|
+
}
|
|
113
|
+
return children;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Refuses what is left at `level`, which is not at its end, where nothing may
|
|
117
|
+
* follow. A missing, split or malformed end-of-contents is what `finish()` says
|
|
118
|
+
* it is (`truncated`, `overrun`, `invalid-end-of-contents`); an element left is
|
|
119
|
+
* `trailing-data` with `message`.
|
|
120
|
+
*/
|
|
121
|
+
function failTrailing(level, message) {
|
|
122
|
+
try {
|
|
123
|
+
level.finish();
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
if (error instanceof DecodingError && error.code === "trailing-data") {
|
|
127
|
+
throw new DecodingError(message, error.offset, "trailing-data", {
|
|
128
|
+
cause: error,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
throw new Error("BerReader: finish() returned with an element left");
|
|
134
|
+
}
|
|
135
|
+
function decodeTaggedContents(context, def, reader) {
|
|
136
|
+
if (effectiveTaggingMode(def, decodingError) === "explicit") {
|
|
137
|
+
requireConstructed(context, reader);
|
|
138
|
+
const inner = enter(context, reader);
|
|
139
|
+
if (inner.atEnd) {
|
|
140
|
+
throw new DecodingError(`${context.label}: EXPLICIT tag with no value inside (X.690 §8.14)`, inner.offset, "missing-element");
|
|
141
|
+
}
|
|
142
|
+
const value = decodeValue(context, def.innerType, inner);
|
|
143
|
+
if (!inner.atEnd) {
|
|
144
|
+
failTrailing(inner, `${context.label}: bytes after the value inside an EXPLICIT tag (X.690 §8.14)`);
|
|
145
|
+
}
|
|
146
|
+
inner.finish();
|
|
147
|
+
return value;
|
|
148
|
+
}
|
|
149
|
+
return decodeContents(context, def.innerType, reader);
|
|
150
|
+
}
|
|
151
|
+
function decodeChoice(context, def, reader) {
|
|
152
|
+
const tagClass = reader.tagClass;
|
|
153
|
+
const tagNumber = reader.tagNumber;
|
|
154
|
+
let fallback;
|
|
155
|
+
for (const alternative of choiceAlternatives(def)) {
|
|
156
|
+
const tags = outerTagSet(alternative.type, decodingError);
|
|
157
|
+
if (tags === undefined) {
|
|
158
|
+
fallback ??= alternative;
|
|
159
|
+
}
|
|
160
|
+
else if (outerTagSetHas(tags, tagClass, tagNumber)) {
|
|
161
|
+
return {
|
|
162
|
+
kind: alternative.name,
|
|
163
|
+
value: decodeValue(context, alternative.type, reader),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (fallback !== undefined) {
|
|
168
|
+
return {
|
|
169
|
+
kind: fallback.name,
|
|
170
|
+
value: decodeValue(context, fallback.type, reader),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
throw new DecodingError(`${context.label}: CHOICE has no alternative for ${describeTag(reader)}`, reader.offset, "unexpected-element");
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Decodes the next element of `reader`, of the universal type `def`, by its
|
|
177
|
+
* contents.
|
|
178
|
+
*/
|
|
179
|
+
function decodeUniversalContents(context, def, reader) {
|
|
180
|
+
if (isStringKind(def.kind)) {
|
|
181
|
+
return decodeString(context, def, reader);
|
|
182
|
+
}
|
|
183
|
+
switch (def.kind) {
|
|
184
|
+
case "bitString": {
|
|
185
|
+
const at = reader.contentsOffset;
|
|
186
|
+
return decodePrimitiveContents(def, reader.readBitStringContents(), at, context.primitive);
|
|
187
|
+
}
|
|
188
|
+
case "sequence": {
|
|
189
|
+
requireConstructed(context, reader);
|
|
190
|
+
const children = enter(context, reader);
|
|
191
|
+
const value = decodeSequence(context, def, children);
|
|
192
|
+
children.finish();
|
|
193
|
+
return value;
|
|
194
|
+
}
|
|
195
|
+
case "set": {
|
|
196
|
+
requireConstructed(context, reader);
|
|
197
|
+
const children = enter(context, reader);
|
|
198
|
+
const value = decodeSet(context, def, children);
|
|
199
|
+
children.finish();
|
|
200
|
+
return value;
|
|
201
|
+
}
|
|
202
|
+
case "sequenceOf":
|
|
203
|
+
case "setOf": {
|
|
204
|
+
requireConstructed(context, reader);
|
|
205
|
+
const children = enter(context, reader);
|
|
206
|
+
const value = [];
|
|
207
|
+
while (!children.atEnd) {
|
|
208
|
+
value.push(decodeValue(context, def.elementType, children));
|
|
209
|
+
}
|
|
210
|
+
children.finish();
|
|
211
|
+
return value;
|
|
212
|
+
}
|
|
213
|
+
case "external":
|
|
214
|
+
case "embeddedPdv":
|
|
215
|
+
case "characterString":
|
|
216
|
+
requireConstructed(context, reader);
|
|
217
|
+
return new Uint8Array(reader.readContents());
|
|
218
|
+
case "any":
|
|
219
|
+
case "openType":
|
|
220
|
+
return { encoded: new Uint8Array(reader.readContents()) };
|
|
221
|
+
default:
|
|
222
|
+
return decodePrimitive(context, def, reader);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
/** The types BER and CER may send as one primitive element only. */
|
|
226
|
+
function decodePrimitive(context, def, reader) {
|
|
227
|
+
requirePrimitive(context, reader);
|
|
228
|
+
const at = reader.contentsOffset;
|
|
229
|
+
return decodePrimitiveContents(def, reader.readContents(), at, context.primitive);
|
|
230
|
+
}
|
|
231
|
+
/** The types encoded as OCTET STRINGs, primitive or as constructed segments. */
|
|
232
|
+
function decodeString(context, def, reader) {
|
|
233
|
+
const start = reader.offset;
|
|
234
|
+
const at = reader.contentsOffset;
|
|
235
|
+
const fresh = reader.constructed;
|
|
236
|
+
const octets = reader.readOctetStringContents();
|
|
237
|
+
if (def.kind === "octetString") {
|
|
238
|
+
// Never `slice()`: on a Buffer it is a view of the input.
|
|
239
|
+
return fresh ? octets : new Uint8Array(octets);
|
|
240
|
+
}
|
|
241
|
+
try {
|
|
242
|
+
return decodePrimitiveContents(def, octets, at, context.primitive);
|
|
243
|
+
}
|
|
244
|
+
catch (error) {
|
|
245
|
+
if (!fresh || !(error instanceof DecodingError))
|
|
246
|
+
throw error;
|
|
247
|
+
// The offset is in the joined segments: place it on the wire.
|
|
248
|
+
throw new DecodingError(error.message, wireOffsetInString(context.bytes, start, error.offset - at), error.code, { cause: error });
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* The offset in the input of octet `joined` of the contents of the constructed
|
|
253
|
+
* string element at `start`, its segments joined (X.690 §8.7.3, §8.23.6); the
|
|
254
|
+
* end of the last segment's contents when there is no such octet. The string
|
|
255
|
+
* was read before, so its framing is known to be valid.
|
|
256
|
+
*/
|
|
257
|
+
function wireOffsetInString(bytes, start, joined) {
|
|
258
|
+
const stack = [new BerReader(bytes.subarray(start), "ber").enter()];
|
|
259
|
+
let remaining = joined;
|
|
260
|
+
let end = start;
|
|
261
|
+
while (stack.length > 0) {
|
|
262
|
+
const top = stack[stack.length - 1];
|
|
263
|
+
if (top.atEnd) {
|
|
264
|
+
top.finish();
|
|
265
|
+
stack.pop();
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
top.peekHeader();
|
|
269
|
+
if (top.constructed) {
|
|
270
|
+
stack.push(top.enter());
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
const contentsOffset = start + top.contentsOffset;
|
|
274
|
+
const length = top.contentsLength;
|
|
275
|
+
if (remaining < length)
|
|
276
|
+
return contentsOffset + remaining;
|
|
277
|
+
remaining -= length;
|
|
278
|
+
end = contentsOffset + length;
|
|
279
|
+
top.skip();
|
|
280
|
+
}
|
|
281
|
+
return end;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* The components of a SEQUENCE in declaration order. An element after the last
|
|
285
|
+
* component is refused, unless the SEQUENCE has a single extension marker: then
|
|
286
|
+
* it is an extension addition this schema does not know. With two markers, the
|
|
287
|
+
* unknown additions are those before the trailing root components.
|
|
288
|
+
*/
|
|
289
|
+
function decodeSequence(context, def, children) {
|
|
290
|
+
const result = {};
|
|
291
|
+
const items = def.components;
|
|
292
|
+
let markers = 0;
|
|
293
|
+
for (let index = 0; index < items.length; index++) {
|
|
294
|
+
const item = items[index];
|
|
295
|
+
if (item.kind === "extensionMarker") {
|
|
296
|
+
markers++;
|
|
297
|
+
if (markers === 2) {
|
|
298
|
+
skipUnknownAdditions(componentList(items.slice(index + 1)), children);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
else if (item.kind === "component") {
|
|
302
|
+
decodeComponent(context, item, children, result);
|
|
303
|
+
}
|
|
304
|
+
else if (item.kind === "extensionAdditionGroup") {
|
|
305
|
+
for (const inner of item.components) {
|
|
306
|
+
decodeComponent(context, inner, children, result);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
while (!children.atEnd) {
|
|
311
|
+
if (markers !== 1) {
|
|
312
|
+
failTrailing(children, `${context.label}: unexpected element after the last component`);
|
|
313
|
+
}
|
|
314
|
+
children.skip();
|
|
315
|
+
}
|
|
316
|
+
return result;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Skips the extension additions this schema does not know, which a newer sender
|
|
320
|
+
* puts before the root components that follow the closing `...` (X.680 §25.4):
|
|
321
|
+
* every element no one of those components can start with. When one of them is
|
|
322
|
+
* an untagged ANY, nothing can be told apart and nothing is skipped.
|
|
323
|
+
*/
|
|
324
|
+
function skipUnknownAdditions(trailing, children) {
|
|
325
|
+
const sets = trailing.map((component) => outerTagSet(component.type, decodingError));
|
|
326
|
+
if (sets.some((tags) => tags === undefined))
|
|
327
|
+
return;
|
|
328
|
+
while (!children.atEnd) {
|
|
329
|
+
children.peekHeader();
|
|
330
|
+
const tagClass = children.tagClass;
|
|
331
|
+
const tagNumber = children.tagNumber;
|
|
332
|
+
if (sets.some((tags) => outerTagSetHas(tags, tagClass, tagNumber)))
|
|
333
|
+
break;
|
|
334
|
+
children.skip();
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* The components of a SET, dispatched by the tag of each element: BER accepts
|
|
339
|
+
* any order, CER requires the order of X.690 §9.3.
|
|
340
|
+
*/
|
|
341
|
+
function decodeSet(context, def, children) {
|
|
342
|
+
const components = componentList(def.components);
|
|
343
|
+
const tagSets = components.map((component) => outerTagSet(component.type, decodingError));
|
|
344
|
+
const present = new Set();
|
|
345
|
+
const result = {};
|
|
346
|
+
const extensible = isExtensible(def);
|
|
347
|
+
const ordered = context.policy.rules === "cer";
|
|
348
|
+
let previousKey;
|
|
349
|
+
for (const component of components) {
|
|
350
|
+
context.policy.checkComponent?.(component);
|
|
351
|
+
}
|
|
352
|
+
while (!children.atEnd) {
|
|
353
|
+
children.peekHeader();
|
|
354
|
+
const tagClass = children.tagClass;
|
|
355
|
+
const tagNumber = children.tagNumber;
|
|
356
|
+
const start = children.offset;
|
|
357
|
+
const index = findSetComponent(components, tagSets, present, tagClass, tagNumber);
|
|
358
|
+
if (index < 0) {
|
|
359
|
+
if (!extensible) {
|
|
360
|
+
throw new DecodingError(`${context.label}: SET has no component for ${describeTag(children)}`, start, "unexpected-element");
|
|
361
|
+
}
|
|
362
|
+
children.skip();
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
const component = components[index];
|
|
366
|
+
if (present.has(component)) {
|
|
367
|
+
throw new DecodingError(`${context.label}: SET component "${component.name}" appears twice`, start, "unexpected-element");
|
|
368
|
+
}
|
|
369
|
+
if (ordered) {
|
|
370
|
+
// An untagged CHOICE sorts as its smallest tag, whichever alternative is sent.
|
|
371
|
+
const key = smallestTagSortKey(component.type, decodingError) ??
|
|
372
|
+
tagSortKey(tagClass, tagNumber ?? children.bigTagNumber());
|
|
373
|
+
if (previousKey !== undefined && key < previousKey) {
|
|
374
|
+
throw new DecodingError("CER: SET components not in ascending tag order (X.690 §9.3)", start, "non-canonical");
|
|
375
|
+
}
|
|
376
|
+
previousKey = key;
|
|
377
|
+
}
|
|
378
|
+
present.add(component);
|
|
379
|
+
setRecordEntry(result, component.name, decodeValue(context, component.type, children));
|
|
380
|
+
}
|
|
381
|
+
for (const component of components) {
|
|
382
|
+
if (!present.has(component)) {
|
|
383
|
+
setAbsent(context, component, children, result, "missing");
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return result;
|
|
387
|
+
}
|
|
388
|
+
function decodeComponent(context, component, children, result) {
|
|
389
|
+
context.policy.checkComponent?.(component);
|
|
390
|
+
if (children.atEnd) {
|
|
391
|
+
setAbsent(context, component, children, result, "missing");
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
children.peekHeader();
|
|
395
|
+
if (!outerTagSetHas(outerTagSet(component.type, decodingError), children.tagClass, children.tagNumber)) {
|
|
396
|
+
setAbsent(context, component, children, result, "tag mismatch");
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
setRecordEntry(result, component.name, decodeValue(context, component.type, children));
|
|
400
|
+
}
|
|
401
|
+
function setAbsent(context, component, children, result, why) {
|
|
402
|
+
if (component.defaultValue !== undefined) {
|
|
403
|
+
setRecordEntry(result, component.name, freshDefault(component));
|
|
404
|
+
}
|
|
405
|
+
else if (!component.optional) {
|
|
406
|
+
throw new DecodingError(`${context.label}: required component "${component.name}" ${why}`, children.offset, "missing-element");
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
//# sourceMappingURL=_decode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_decode.js","sourceRoot":"","sources":["../../src/ber/_decode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAiB,MAAM,cAAc,CAAC;AAE7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EACN,uBAAuB,GAEvB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EACN,kBAAkB,EAClB,aAAa,EACb,oBAAoB,EACpB,gBAAgB,EAChB,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,cAAc,EACd,kBAAkB,EAClB,SAAS,GACT,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAOpD,OAAO,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAW5C,MAAM,CAAC,MAAM,UAAU,GAAoB;IAC1C,KAAK,EAAE,KAAK;IACZ,aAAa,EAAE,KAAK;IACpB,cAAc,EAAE,SAAS;CACzB,CAAC;AAEF,MAAM,CAA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,SAAS,CAAC,QAAQ,EAAE,CAAC;QAChC,MAAM,IAAI,aAAa,CACtB,GAAG,OAAO,CAAC,KAAK,yBAAyB,SAAS,CAAC,IAAI,KAAK,GAAG,EAAE,EACjE,QAAQ,CAAC,MAAM,EACf,iBAAiB,CACjB,CAAC;IACH,CAAC;AACF,CAAC"}
|
package/dist/ber/codec.d.ts
CHANGED
|
@@ -14,10 +14,11 @@ import { type BerEncodeOptions } from "./encode.js";
|
|
|
14
14
|
* bytes one value occupies, so an incomplete value is reported as such with a
|
|
15
15
|
* `needAtLeast` hint instead of being guessed at. Indefinite-length encodings
|
|
16
16
|
* are decoded only once the whole input is available, since their extent is not
|
|
17
|
-
* declared up front.
|
|
17
|
+
* declared up front; then `consumed` is the bytes the value used.
|
|
18
18
|
*
|
|
19
19
|
* A malformed value is `fatal`: the length that framed it cannot be trusted, so
|
|
20
|
-
* there is no position to resynchronise from.
|
|
20
|
+
* there is no position to resynchronise from. So is a header declaring more
|
|
21
|
+
* than `Number.MAX_SAFE_INTEGER` octets in all (`limit-exceeded`).
|
|
21
22
|
*/
|
|
22
23
|
export declare function createBerDecoder<D extends AnyAsn1TypeDef>(def: D, options?: BerDecodeOptions): Decoder<DefValueOf<D>>;
|
|
23
24
|
/**
|
package/dist/ber/codec.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/ber/codec.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/ber/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,OAAO,EAEZ,KAAK,OAAO,EAEZ,MAAM,qBAAqB,CAAC;AAS7B,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAOjE,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,KAAK,gBAAgB,EAAa,MAAM,aAAa,CAAC;AA8C/D;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,cAAc,EACxD,GAAG,EAAE,CAAC,EACN,OAAO,GAAE,gBAAqB,GAC5B,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CA+BxB;AAsBD;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,cAAc,EACxD,GAAG,EAAE,CAAC,EACN,OAAO,GAAE,gBAAqB,GAC5B,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAIxB"}
|
package/dist/ber/codec.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ByteReader } from "@ac-kit/core";
|
|
2
1
|
import { decodeIncomplete, } from "@ac-kit/format-core";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { DecodingError } from "../errors.js";
|
|
3
|
+
import { createHeaderScratch, DEFAULT_MAX_TAG_OCTETS, readHeaderAt, } from "../reader/_header.js";
|
|
4
|
+
import { BER_POLICY, BER_STRICT_POLICY, decodeBerFamily, } from "./_decode.js";
|
|
5
5
|
import { berEncode } from "./encode.js";
|
|
6
6
|
/**
|
|
7
7
|
* Total byte length of the TLV at the front of `view`, or `undefined` when the
|
|
@@ -10,18 +10,36 @@ import { berEncode } from "./encode.js";
|
|
|
10
10
|
* Indefinite-length encodings are reported as `undefined` too: their extent is
|
|
11
11
|
* only known once the matching end-of-contents marker is found, which is a scan
|
|
12
12
|
* the framing layer must not do speculatively.
|
|
13
|
+
*
|
|
14
|
+
* @throws {DecodingError} `limit-exceeded` when the header and the contents it
|
|
15
|
+
* declares are together longer than the largest safe integer: no input can
|
|
16
|
+
* ever complete such a value, and the total is no number a hint can carry.
|
|
13
17
|
*/
|
|
14
|
-
function framedLength(view) {
|
|
18
|
+
function framedLength(view, scratch) {
|
|
19
|
+
if (view.length === 0) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
15
22
|
try {
|
|
16
|
-
|
|
17
|
-
|
|
23
|
+
readHeaderAt(view, 0, view.length, true, DEFAULT_MAX_TAG_OCTETS, scratch);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
if (error instanceof DecodingError) {
|
|
27
|
+
// More input mends the others, not a limit.
|
|
28
|
+
if (error.code === "limit-exceeded")
|
|
29
|
+
throw error;
|
|
18
30
|
return undefined;
|
|
19
31
|
}
|
|
20
|
-
|
|
32
|
+
throw error;
|
|
21
33
|
}
|
|
22
|
-
|
|
34
|
+
const length = scratch.contentsLength;
|
|
35
|
+
if (length < 0) {
|
|
23
36
|
return undefined;
|
|
24
37
|
}
|
|
38
|
+
const headerLength = scratch.contentsOffset;
|
|
39
|
+
if (length > Number.MAX_SAFE_INTEGER - headerLength) {
|
|
40
|
+
throw new DecodingError(`Element of ${length} content octet(s) and a ${headerLength}-octet header is longer than the largest safe integer`, scratch.lengthOffset, "limit-exceeded");
|
|
41
|
+
}
|
|
42
|
+
return headerLength + length;
|
|
25
43
|
}
|
|
26
44
|
/**
|
|
27
45
|
* Creates a BER {@link Decoder} bound to `def`.
|
|
@@ -34,36 +52,47 @@ function framedLength(view) {
|
|
|
34
52
|
* bytes one value occupies, so an incomplete value is reported as such with a
|
|
35
53
|
* `needAtLeast` hint instead of being guessed at. Indefinite-length encodings
|
|
36
54
|
* are decoded only once the whole input is available, since their extent is not
|
|
37
|
-
* declared up front.
|
|
55
|
+
* declared up front; then `consumed` is the bytes the value used.
|
|
38
56
|
*
|
|
39
57
|
* A malformed value is `fatal`: the length that framed it cannot be trusted, so
|
|
40
|
-
* there is no position to resynchronise from.
|
|
58
|
+
* there is no position to resynchronise from. So is a header declaring more
|
|
59
|
+
* than `Number.MAX_SAFE_INTEGER` octets in all (`limit-exceeded`).
|
|
41
60
|
*/
|
|
42
61
|
export function createBerDecoder(def, options = {}) {
|
|
62
|
+
const policy = options.strict ? BER_STRICT_POLICY : BER_POLICY;
|
|
63
|
+
const scratch = createHeaderScratch();
|
|
43
64
|
return {
|
|
44
65
|
decode(view, context) {
|
|
45
|
-
|
|
66
|
+
let total;
|
|
67
|
+
try {
|
|
68
|
+
total = framedLength(view, scratch);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
// A framing error that no further input can mend.
|
|
72
|
+
return { status: "fatal", error };
|
|
73
|
+
}
|
|
46
74
|
if (total === undefined) {
|
|
47
75
|
if (!context.atEof) {
|
|
48
76
|
return { status: "incomplete" };
|
|
49
77
|
}
|
|
50
78
|
// At end of input the buffer is all there will ever be, so an
|
|
51
79
|
// indefinite-length value can finally be decoded against it.
|
|
52
|
-
return decodeSlice(def, view,
|
|
80
|
+
return decodeSlice(def, view, policy);
|
|
53
81
|
}
|
|
54
82
|
if (view.length < total) {
|
|
55
83
|
return decodeIncomplete(total);
|
|
56
84
|
}
|
|
57
|
-
return decodeSlice(def, view.subarray(0, total),
|
|
85
|
+
return decodeSlice(def, view.subarray(0, total), policy);
|
|
58
86
|
},
|
|
59
87
|
};
|
|
60
88
|
}
|
|
61
|
-
function decodeSlice(def, slice,
|
|
89
|
+
function decodeSlice(def, slice, policy) {
|
|
62
90
|
try {
|
|
91
|
+
const { value, end } = decodeBerFamily(def, slice, policy);
|
|
63
92
|
return {
|
|
64
93
|
status: "decoded",
|
|
65
|
-
value:
|
|
66
|
-
consumed,
|
|
94
|
+
value: value,
|
|
95
|
+
consumed: end,
|
|
67
96
|
};
|
|
68
97
|
}
|
|
69
98
|
catch (error) {
|
package/dist/ber/codec.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codec.js","sourceRoot":"","sources":["../../src/ber/codec.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"codec.js","sourceRoot":"","sources":["../../src/ber/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAKN,gBAAgB,GAChB,M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