@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.
Files changed (105) hide show
  1. package/CHANGELOG.md +81 -0
  2. package/README.md +132 -4
  3. package/dist/_encoding/constants.d.ts +2 -0
  4. package/dist/_encoding/constants.d.ts.map +1 -1
  5. package/dist/_encoding/constants.js +7 -0
  6. package/dist/_encoding/constants.js.map +1 -1
  7. package/dist/_encoding/decode-primitive.d.ts +39 -0
  8. package/dist/_encoding/decode-primitive.d.ts.map +1 -0
  9. package/dist/_encoding/decode-primitive.js +144 -0
  10. package/dist/_encoding/decode-primitive.js.map +1 -0
  11. package/dist/_encoding/def-tags.d.ts +36 -3
  12. package/dist/_encoding/def-tags.d.ts.map +1 -1
  13. package/dist/_encoding/def-tags.js +88 -3
  14. package/dist/_encoding/def-tags.js.map +1 -1
  15. package/dist/_encoding/encoded-tag-sort-key.d.ts +7 -0
  16. package/dist/_encoding/encoded-tag-sort-key.d.ts.map +1 -0
  17. package/dist/_encoding/encoded-tag-sort-key.js +13 -0
  18. package/dist/_encoding/encoded-tag-sort-key.js.map +1 -0
  19. package/dist/_encoding/tag.d.ts +1 -6
  20. package/dist/_encoding/tag.d.ts.map +1 -1
  21. package/dist/_encoding/tag.js +5 -43
  22. package/dist/_encoding/tag.js.map +1 -1
  23. package/dist/_encoding/tlv.d.ts +4 -25
  24. package/dist/_encoding/tlv.d.ts.map +1 -1
  25. package/dist/_encoding/tlv.js +12 -48
  26. package/dist/_encoding/tlv.js.map +1 -1
  27. package/dist/_encoding/values.d.ts.map +1 -1
  28. package/dist/_encoding/values.js +31 -19
  29. package/dist/_encoding/values.js.map +1 -1
  30. package/dist/_encoding/vlq.d.ts.map +1 -1
  31. package/dist/_encoding/vlq.js +2 -2
  32. package/dist/_encoding/vlq.js.map +1 -1
  33. package/dist/_encoding/with-offset.d.ts +12 -0
  34. package/dist/_encoding/with-offset.d.ts.map +1 -0
  35. package/dist/_encoding/with-offset.js +22 -0
  36. package/dist/_encoding/with-offset.js.map +1 -0
  37. package/dist/ber/_decode.d.ts +25 -0
  38. package/dist/ber/_decode.d.ts.map +1 -0
  39. package/dist/ber/_decode.js +409 -0
  40. package/dist/ber/_decode.js.map +1 -0
  41. package/dist/ber/codec.d.ts +3 -2
  42. package/dist/ber/codec.d.ts.map +1 -1
  43. package/dist/ber/codec.js +45 -16
  44. package/dist/ber/codec.js.map +1 -1
  45. package/dist/ber/decode.d.ts +30 -1
  46. package/dist/ber/decode.d.ts.map +1 -1
  47. package/dist/ber/decode.js +31 -221
  48. package/dist/ber/decode.js.map +1 -1
  49. package/dist/ber/encode.d.ts +7 -1
  50. package/dist/ber/encode.d.ts.map +1 -1
  51. package/dist/ber/encode.js +46 -53
  52. package/dist/ber/encode.js.map +1 -1
  53. package/dist/cer/decode.d.ts +20 -1
  54. package/dist/cer/decode.d.ts.map +1 -1
  55. package/dist/cer/decode.js +27 -221
  56. package/dist/cer/decode.js.map +1 -1
  57. package/dist/cer/encode.d.ts +22 -6
  58. package/dist/cer/encode.d.ts.map +1 -1
  59. package/dist/cer/encode.js +147 -136
  60. package/dist/cer/encode.js.map +1 -1
  61. package/dist/der/decode.d.ts +27 -21
  62. package/dist/der/decode.d.ts.map +1 -1
  63. package/dist/der/decode.js +323 -398
  64. package/dist/der/decode.js.map +1 -1
  65. package/dist/der/encode.js +5 -8
  66. package/dist/der/encode.js.map +1 -1
  67. package/dist/errors.d.ts +56 -0
  68. package/dist/errors.d.ts.map +1 -0
  69. package/dist/errors.js +31 -0
  70. package/dist/errors.js.map +1 -0
  71. package/dist/index.d.ts +4 -0
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/index.js +4 -0
  74. package/dist/index.js.map +1 -1
  75. package/dist/per/decode.d.ts +9 -0
  76. package/dist/per/decode.d.ts.map +1 -1
  77. package/dist/per/decode.js +114 -32
  78. package/dist/per/decode.js.map +1 -1
  79. package/dist/per/encode.js +1 -1
  80. package/dist/per/encode.js.map +1 -1
  81. package/dist/reader/_framing.d.ts +38 -0
  82. package/dist/reader/_framing.d.ts.map +1 -0
  83. package/dist/reader/_framing.js +144 -0
  84. package/dist/reader/_framing.js.map +1 -0
  85. package/dist/reader/_header.d.ts +49 -0
  86. package/dist/reader/_header.d.ts.map +1 -0
  87. package/dist/reader/_header.js +130 -0
  88. package/dist/reader/_header.js.map +1 -0
  89. package/dist/reader/ber-reader.d.ts +240 -0
  90. package/dist/reader/ber-reader.d.ts.map +1 -0
  91. package/dist/reader/ber-reader.js +567 -0
  92. package/dist/reader/ber-reader.js.map +1 -0
  93. package/dist/reader/inspect.d.ts +68 -0
  94. package/dist/reader/inspect.d.ts.map +1 -0
  95. package/dist/reader/inspect.js +104 -0
  96. package/dist/reader/inspect.js.map +1 -0
  97. package/dist/schema/universal-tag-name.d.ts +26 -0
  98. package/dist/schema/universal-tag-name.d.ts.map +1 -0
  99. package/dist/schema/universal-tag-name.js +74 -0
  100. package/dist/schema/universal-tag-name.js.map +1 -0
  101. package/package.json +9 -8
  102. package/dist/_encoding/errors.d.ts +0 -8
  103. package/dist/_encoding/errors.d.ts.map +0 -1
  104. package/dist/_encoding/errors.js +0 -15
  105. package/dist/_encoding/errors.js.map +0 -1
@@ -0,0 +1,240 @@
1
+ import { type Callable } from "@ac-kit/core";
2
+ import type { TagClass } from "../schema/metadata.js";
3
+ /** The X.690 encoding rules: BER, and its canonical subsets CER and DER. */
4
+ export type X690Rules = "ber" | "cer" | "der";
5
+ /** A violation of the CER or DER rules, reported instead of thrown. */
6
+ export type BerIssue = {
7
+ /**
8
+ * Offset of the offending octet, relative to the view given to the root
9
+ * reader.
10
+ */
11
+ readonly offset: number;
12
+ readonly message: string;
13
+ };
14
+ export type BerReaderOptions = {
15
+ /**
16
+ * Identifier octets allowed after the first, for a high tag number: a
17
+ * positive safe integer, default 16 (tag numbers up to 2¹¹² − 1).
18
+ */
19
+ readonly maxTagOctets?: number;
20
+ /**
21
+ * Receives each violation of the CER or DER rules (code `non-canonical`)
22
+ * instead of having it thrown; reading then continues as BER would. Never
23
+ * called with `rules` `"ber"`. An exception it throws propagates. It must not
24
+ * call the reader or its children: every call made from it throws
25
+ * `TypeError`.
26
+ */
27
+ readonly onNonCanonical?: Callable<[issue: BerIssue]>;
28
+ };
29
+ /**
30
+ * A forward-only cursor over BER, CER or DER bytes (X.690 §8–§10).
31
+ *
32
+ * One reader reads the elements of one level: the whole view for the reader the
33
+ * constructor returns, the contents of a constructed element for one `enter()`
34
+ * returns. `peekHeader()` reads the next element's identifier and length octets
35
+ * without consuming it; `readContents()`, `readElement()`, `skip()`, `enter()`,
36
+ * `readOctetStringContents()` or `readBitStringContents()` then consumes it.
37
+ * Every offset is relative to the view given to the root reader's constructor,
38
+ * children included.
39
+ *
40
+ * End-of-contents octets are recognised only where an indefinite-length element
41
+ * may end. Nothing is copied except by the two string helpers for a constructed
42
+ * string. The reader holds no resource: no `Symbol.dispose`.
43
+ *
44
+ * A method that throws (a `DecodingError`, or what `onNonCanonical` throws)
45
+ * consumes nothing: the reader stays open at the same offset, with no child
46
+ * open, so the element can be read again, with other rules, or the level ended
47
+ * with `finish()`. The current header, if any, may be gone: peek it again.
48
+ *
49
+ * @example
50
+ * // The bytes a certificate's signature covers, as received.
51
+ * const certificate = new BerReader(der, "der").enter();
52
+ * const tbsCertificate = certificate.readElement();
53
+ */
54
+ export declare class BerReader<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike> {
55
+ /** The rules this reader and every reader `enter()` derives from it enforce. */
56
+ readonly rules: X690Rules;
57
+ /** 0 for a reader made by the constructor; its parent's depth + 1 otherwise. */
58
+ readonly depth: number;
59
+ private readonly shared_;
60
+ private readonly parent_;
61
+ private readonly bound_;
62
+ private readonly indefinite_;
63
+ private readonly inputEnd_;
64
+ private position_;
65
+ private state_;
66
+ /**
67
+ * @param bytes - The view to read; not copied, so it must not change while a
68
+ * reader of it is in use. What a reader has parsed is not read again: a
69
+ * change cannot make a header's getters disagree, but the result of later
70
+ * reads is unspecified.
71
+ * @param rules - The rules to enforce.
72
+ * @param options - See {@link BerReaderOptions}.
73
+ * @throws {RangeError} When `rules` is not one of the three, or
74
+ * `options.maxTagOctets` is not a positive safe integer.
75
+ */
76
+ constructor(bytes: Uint8Array<TArrayBuffer>, rules: X690Rules, options?: BerReaderOptions);
77
+ /**
78
+ * Offset of the next element of this level: the start of the current header's
79
+ * element while one is current, the end of the last consumed one otherwise.
80
+ *
81
+ * @throws {TypeError} While a child is open, or after `finish()`.
82
+ */
83
+ get offset(): number;
84
+ /**
85
+ * Whether no element is left at this level: the end of the view (root), of
86
+ * the contents (definite length), or end-of-contents octets next (indefinite
87
+ * length). `false` while a header is current. O(1); consumes nothing.
88
+ *
89
+ * @throws {TypeError} While a child is open, or after `finish()`.
90
+ */
91
+ get atEnd(): boolean;
92
+ /**
93
+ * Reads the identifier and length octets of the next element and makes them
94
+ * the current header, without consuming the element. Returns at once while a
95
+ * header is current. O(identifier + length octets); allocates nothing for a
96
+ * low tag number (one `ByteReader` for a high one).
97
+ *
98
+ * @throws {DecodingError} `missing-element` (none left at a level inside an
99
+ * element), `truncated` (none left in the view, or the input ends inside
100
+ * the header or contents), `overrun`, `invalid-tag`, `limit-exceeded`,
101
+ * `invalid-length`, `invalid-end-of-contents`, or `non-canonical` (the
102
+ * rules forbid the length form; not with `onNonCanonical`).
103
+ * @throws {TypeError} While a child is open, or after `finish()`.
104
+ */
105
+ peekHeader(): void;
106
+ /**
107
+ * Class of the current header.
108
+ *
109
+ * @throws {TypeError} When none is current.
110
+ */
111
+ get tagClass(): TagClass;
112
+ /**
113
+ * Tag number of the current header, or `null` above `Number.MAX_SAFE_INTEGER`
114
+ * (see {@link BerReader.bigTagNumber}).
115
+ *
116
+ * @throws {TypeError} When none is current.
117
+ */
118
+ get tagNumber(): number | null;
119
+ /**
120
+ * Exact tag number of the current header: the one `tagNumber` gives, as a
121
+ * `bigint`, or the larger one it hides. Allocates.
122
+ *
123
+ * @throws {TypeError} When none is current.
124
+ */
125
+ bigTagNumber(): bigint;
126
+ /**
127
+ * Whether the current header has the constructed bit.
128
+ *
129
+ * @throws {TypeError} When none is current.
130
+ */
131
+ get constructed(): boolean;
132
+ /**
133
+ * Offset of the current element's contents.
134
+ *
135
+ * @throws {TypeError} When none is current.
136
+ */
137
+ get contentsOffset(): number;
138
+ /**
139
+ * Length of the current element's contents, or `null` for the indefinite
140
+ * form.
141
+ *
142
+ * @throws {TypeError} When none is current.
143
+ */
144
+ get contentsLength(): number | null;
145
+ /**
146
+ * Consumes the next element and returns its contents octets as a view,
147
+ * without the end-of-contents octets. O(1) for a definite length; for the
148
+ * indefinite form O(element size) time and O(1) memory, checking the framing
149
+ * of the indefinite-length elements nested in it (not of what lies inside
150
+ * nested definite-length ones).
151
+ *
152
+ * @throws {DecodingError} As {@link BerReader.peekHeader}, and for what the
153
+ * walk finds.
154
+ * @throws {TypeError} While a child is open, or after `finish()`.
155
+ */
156
+ readContents(): Uint8Array<TArrayBuffer>;
157
+ /**
158
+ * As {@link BerReader.readContents}, returning the whole element: header,
159
+ * contents and end-of-contents octets.
160
+ */
161
+ readElement(): Uint8Array<TArrayBuffer>;
162
+ /** As {@link BerReader.readContents}, returning nothing. */
163
+ skip(): void;
164
+ /**
165
+ * Consumes the next element, which must be constructed, and returns a reader
166
+ * over its contents. This reader then throws `TypeError` until the child's
167
+ * `finish()`, after which it continues past the element. O(1); allocates the
168
+ * child.
169
+ *
170
+ * @throws {DecodingError} As {@link BerReader.peekHeader};
171
+ * `unexpected-element` for a primitive element.
172
+ * @throws {TypeError} While a child is open, or after `finish()`.
173
+ */
174
+ enter(): BerReader<TArrayBuffer>;
175
+ /**
176
+ * Ends this reader: checks no element is left, consumes the end-of-contents
177
+ * octets of an indefinite-length level, and lets the parent continue after
178
+ * the element. On a root reader, checks the whole view was read.
179
+ *
180
+ * @throws {DecodingError} `trailing-data` (an element is left, also when its
181
+ * header was peeked but not consumed); `truncated` or `overrun` (an
182
+ * indefinite-length level reaches its bound, or ends in a single `00`,
183
+ * without end-of-contents octets); `invalid-end-of-contents` (octets that
184
+ * begin like them but are not `00 00`).
185
+ * @throws {TypeError} While a child is open, after `finish()`, or from
186
+ * `onNonCanonical`.
187
+ */
188
+ finish(): void;
189
+ /**
190
+ * Consumes the next element as a string encoded like an OCTET STRING (OCTET
191
+ * STRING, the restricted character strings, UTCTime, GeneralizedTime)
192
+ * whatever its tag, and returns the contents of its primitive form: a view
193
+ * for a primitive element, a fresh array joining the segments of a
194
+ * constructed one (X.690 §8.7.3, §8.23). Each segment must be an OCTET STRING
195
+ * (UNIVERSAL 4); BER allows constructed segments, nested at most 512 levels
196
+ * deep. CER (§9.2): the primitive form if and only if 1000 octets or fewer,
197
+ * primitive segments of exactly 1000 octets but the last (1 to 1000). DER
198
+ * (§10.2): primitive only. O(element size).
199
+ *
200
+ * @throws {DecodingError} As {@link BerReader.readContents};
201
+ * `unexpected-element` for a segment that is not an OCTET STRING;
202
+ * `limit-exceeded` for segments nested deeper than 512 levels;
203
+ * `non-canonical` for a CER or DER violation (not with `onNonCanonical`).
204
+ */
205
+ readOctetStringContents(): Uint8Array<TArrayBuffer | ArrayBuffer>;
206
+ /**
207
+ * As {@link BerReader.readOctetStringContents} for a BIT STRING (X.690
208
+ * §8.6.4): segments are BIT STRINGs (UNIVERSAL 3) with at least the
209
+ * unused-bits octet, a segment with unused bits being the last one at every
210
+ * level (no segment, however empty, follows it), and the result is the
211
+ * primitive contents: the last segment's unused-bits octet, then every
212
+ * segment's bits (`00` for a constructed BIT STRING with no segment). CER
213
+ * segments hold exactly 1000 contents octets but the last. The unused-bits
214
+ * value and padding are the value decoder's to check.
215
+ *
216
+ * @throws {DecodingError} As {@link BerReader.readOctetStringContents};
217
+ * `invalid-contents` for a segment without its unused-bits octet or with
218
+ * unused bits before the last.
219
+ */
220
+ readBitStringContents(): Uint8Array<TArrayBuffer | ArrayBuffer>;
221
+ private assertOpen_;
222
+ /**
223
+ * Reports a violation met while this reader holds a current header, with the
224
+ * reader refusing calls from the caller's callback.
225
+ */
226
+ private report_;
227
+ private assertHeader_;
228
+ /**
229
+ * Consumes the current header's element and returns the offset where its
230
+ * contents end, before any end-of-contents octets.
231
+ */
232
+ private advance_;
233
+ private readString_;
234
+ /**
235
+ * Joins the segments of the constructed string whose header is current, at
236
+ * `start`.
237
+ */
238
+ private joinSegments_;
239
+ }
240
+ //# sourceMappingURL=ber-reader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ber-reader.d.ts","sourceRoot":"","sources":["../../src/reader/ber-reader.ts"],"names":[],"mappings":"AAAA,OAAO,EAIN,KAAK,QAAQ,EACb,MAAM,cAAc,CAAC;AAOtB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAatD,4EAA4E;AAC5E,MAAM,MAAM,SAAS,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC;AAE9C,uEAAuE;AACvE,MAAM,MAAM,QAAQ,GAAG;IACtB;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC9B;;;OAGG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B;;;;;;OAMG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,QAAQ,CAAC,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC;CACtD,CAAC;AAqDF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,SAAS,CAAC,YAAY,SAAS,eAAe,GAAG,eAAe;IAC5E,gFAAgF;IAChF,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;IAC1B,gFAAgF;IAChF,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CA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