@lapo/asn1js 1.2.4 → 2.0.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/asn1.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // ASN.1 JavaScript decoder
2
- // Copyright (c) 2008-2022 Lapo Luchini <lapo@lapo.it>
2
+ // Copyright (c) 2008-2024 Lapo Luchini <lapo@lapo.it>
3
3
 
4
4
  // Permission to use, copy, modify, and/or distribute this software for any
5
5
  // purpose with or without fee is hereby granted, provided that the above
@@ -13,17 +13,33 @@
13
13
  // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
14
  // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
15
 
16
- (typeof define != 'undefined' ? define : function (factory) { 'use strict';
17
- if (typeof module == 'object') module.exports = factory(function (name) { return require(name); });
18
- else window.asn1 = factory(function (name) { return window[name.substring(2)]; });
19
- })(function (require) {
20
- "use strict";
16
+ import { Int10 } from './int10.js';
17
+ import { oids } from './oids.js';
21
18
 
22
- var Int10 = require('./int10'),
23
- oids = require('./oids'),
24
- ellipsis = "\u2026",
19
+ const
20
+ ellipsis = '\u2026',
25
21
  reTimeS = /^(\d\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])(?:([0-5]\d)(?:([0-5]\d)(?:[.,](\d{1,3}))?)?)?(Z|(-(?:0\d|1[0-2])|[+](?:0\d|1[0-4]))([0-5]\d)?)?$/,
26
- reTimeL = /^(\d\d\d\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])(?:([0-5]\d)(?:([0-5]\d)(?:[.,](\d{1,3}))?)?)?(Z|(-(?:0\d|1[0-2])|[+](?:0\d|1[0-4]))([0-5]\d)?)?$/;
22
+ reTimeL = /^(\d\d\d\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])(?:([0-5]\d)(?:([0-5]\d)(?:[.,](\d{1,3}))?)?)?(Z|(-(?:0\d|1[0-2])|[+](?:0\d|1[0-4]))([0-5]\d)?)?$/,
23
+ hexDigits = '0123456789ABCDEF',
24
+ b64Safe = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
25
+ tableT61 = [
26
+ ['', ''],
27
+ ['AEIOUaeiou', 'ÀÈÌÒÙàèìòù'], // Grave
28
+ ['ACEILNORSUYZacegilnorsuyz', 'ÁĆÉÍĹŃÓŔŚÚÝŹáćéģíĺńóŕśúýź'], // Acute
29
+ ['ACEGHIJOSUWYaceghijosuwy', 'ÂĈÊĜĤÎĴÔŜÛŴŶâĉêĝĥîĵôŝûŵŷ'], // Circumflex
30
+ ['AINOUainou', 'ÃĨÑÕŨãĩñõũ'], // Tilde
31
+ ['AEIOUaeiou', 'ĀĒĪŌŪāēīōū'], // Macron
32
+ ['AGUagu', 'ĂĞŬăğŭ'], // Breve
33
+ ['CEGIZcegz', 'ĊĖĠİŻċėġż'], // Dot
34
+ ['AEIOUYaeiouy', 'ÄËÏÖÜŸäëïöüÿ'], // Umlaut or diæresis
35
+ ['', ''],
36
+ ['AUau', 'ÅŮåů'], // Ring
37
+ ['CGKLNRSTcklnrst', 'ÇĢĶĻŅŖŞŢçķļņŗşţ'], // Cedilla
38
+ ['', ''],
39
+ ['OUou', 'ŐŰőű'], // Double Acute
40
+ ['AEIUaeiu', 'ĄĘĮŲąęįų'], // Ogonek
41
+ ['CDELNRSTZcdelnrstz', 'ČĎĚĽŇŘŠŤŽčďěľňřšťž'] // Caron
42
+ ];
27
43
 
28
44
  function stringCut(str, len) {
29
45
  if (str.length > len)
@@ -31,348 +47,284 @@ function stringCut(str, len) {
31
47
  return str;
32
48
  }
33
49
 
34
- function Stream(enc, pos) {
35
- if (enc instanceof Stream) {
36
- this.enc = enc.enc;
37
- this.pos = enc.pos;
38
- } else {
39
- // enc should be an array or a binary string
40
- this.enc = enc;
41
- this.pos = pos;
42
- }
43
- }
44
- Stream.prototype.get = function (pos) {
45
- if (pos === undefined)
46
- pos = this.pos++;
47
- if (pos >= this.enc.length)
48
- throw 'Requesting byte offset ' + pos + ' on a stream of length ' + this.enc.length;
49
- return (typeof this.enc == "string") ? this.enc.charCodeAt(pos) : this.enc[pos];
50
- };
51
- Stream.prototype.hexDigits = "0123456789ABCDEF";
52
- Stream.prototype.hexByte = function (b) {
53
- return this.hexDigits.charAt((b >> 4) & 0xF) + this.hexDigits.charAt(b & 0xF);
54
- };
55
- Stream.prototype.hexDump = function (start, end, raw) {
56
- var s = "";
57
- for (var i = start; i < end; ++i) {
58
- s += this.hexByte(this.get(i));
59
- if (raw !== true)
60
- switch (i & 0xF) {
61
- case 0x7: s += " "; break;
62
- case 0xF: s += "\n"; break;
63
- default: s += " ";
64
- }
65
- }
66
- return s;
67
- };
68
- var b64Safe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
69
- Stream.prototype.b64Dump = function (start, end) {
70
- var extra = (end - start) % 3,
71
- s = '',
72
- i, c;
73
- for (i = start; i + 2 < end; i += 3) {
74
- c = this.get(i) << 16 | this.get(i + 1) << 8 | this.get(i + 2);
75
- s += b64Safe.charAt(c >> 18 & 0x3F);
76
- s += b64Safe.charAt(c >> 12 & 0x3F);
77
- s += b64Safe.charAt(c >> 6 & 0x3F);
78
- s += b64Safe.charAt(c & 0x3F);
79
- }
80
- if (extra > 0) {
81
- c = this.get(i) << 16;
82
- if (extra > 1) c |= this.get(i + 1) << 8;
83
- s += b64Safe.charAt(c >> 18 & 0x3F);
84
- s += b64Safe.charAt(c >> 12 & 0x3F);
85
- if (extra == 2) s += b64Safe.charAt(c >> 6 & 0x3F);
86
- }
87
- return s;
88
- };
89
- Stream.prototype.isASCII = function (start, end) {
90
- for (var i = start; i < end; ++i) {
91
- var c = this.get(i);
92
- if (c < 32 || c > 176)
93
- return false;
94
- }
95
- return true;
96
- };
97
- Stream.prototype.parseStringISO = function (start, end, maxLength) {
98
- var s = "";
99
- for (var i = start; i < end; ++i)
100
- s += String.fromCharCode(this.get(i));
101
- return { size: s.length, str: stringCut(s, maxLength) };
102
- };
103
- var tableT61 = [
104
- ['', ''],
105
- ['AEIOUaeiou', 'ÀÈÌÒÙàèìòù'], // Grave
106
- ['ACEILNORSUYZacegilnorsuyz', 'ÁĆÉÍĹŃÓŔŚÚÝŹáćéģíĺńóŕśúýź'], // Acute
107
- ['ACEGHIJOSUWYaceghijosuwy', 'ÂĈÊĜĤÎĴÔŜÛŴŶâĉêĝĥîĵôŝûŵŷ'], // Circumflex
108
- ['AINOUainou', 'ÃĨÑÕŨãĩñõũ'], // Tilde
109
- ['AEIOUaeiou', 'ĀĒĪŌŪāēīōū'], // Macron
110
- ['AGUagu', 'ĂĞŬăğŭ'], // Breve
111
- ['CEGIZcegz', 'ĊĖĠİŻċėġż'], // Dot
112
- ['AEIOUYaeiouy', 'ÄËÏÖÜŸäëïöüÿ'], // Umlaut or diæresis
113
- ['', ''],
114
- ['AUau', 'ÅŮåů'], // Ring
115
- ['CGKLNRSTcklnrst', 'ÇĢĶĻŅŖŞŢçķļņŗşţ'], // Cedilla
116
- ['', ''],
117
- ['OUou', 'ŐŰőű'], // Double Acute
118
- ['AEIUaeiu', 'ĄĘĮŲąęįų'], // Ogonek
119
- ['CDELNRSTZcdelnrstz', 'ČĎĚĽŇŘŠŤŽčďěľňřšťž'] // Caron
120
- ];
121
- Stream.prototype.parseStringT61 = function (start, end, maxLength) {
122
- // warning: this code is not very well tested so far
123
- function merge(c, d) {
124
- var t = tableT61[c - 0xC0];
125
- var i = t[0].indexOf(String.fromCharCode(d));
126
- return (i < 0) ? '\0' : t[1].charAt(i);
127
- }
128
- var s = "", c;
129
- for (var i = start; i < end; ++i) {
130
- c = this.get(i);
131
- if (c >= 0xA4 && c <= 0xBF)
132
- s += '$¥#§¤\0\0«\0\0\0\0°±²³×µ¶·÷\0\0»¼½¾¿'.charAt(c - 0xA4);
133
- else if (c >= 0xE0 && c <= 0xFF)
134
- s += 'ΩÆÐªĦ\0IJĿŁØŒºÞŦŊʼnĸæđðħıijŀłøœßþŧŋ\0'.charAt(c - 0xE0);
135
- else if (c >= 0xC0 && c <= 0xCF)
136
- s += merge(c, this.get(++i));
137
- else // using ISO 8859-1 for characters undefined (or equal) in T.61
138
- s += String.fromCharCode(c);
139
- }
140
- return { size: s.length, str: stringCut(s, maxLength) };
141
- };
142
- Stream.prototype.parseStringUTF = function (start, end, maxLength) {
143
- function ex(c) { // must be 10xxxxxx
144
- if ((c < 0x80) || (c >= 0xC0))
145
- throw new Error('Invalid UTF-8 continuation byte: ' + c);
146
- return (c & 0x3F);
147
- }
148
- function surrogate(cp) {
149
- if (cp < 0x10000)
150
- throw new Error('UTF-8 overlong encoding, codepoint encoded in 4 bytes: ' + cp);
151
- // we could use String.fromCodePoint(cp) but let's be nice to older browsers and use surrogate pairs
152
- cp -= 0x10000;
153
- return String.fromCharCode((cp >> 10) + 0xD800, (cp & 0x3FF) + 0xDC00);
154
- }
155
- var s = "";
156
- for (var i = start; i < end; ) {
157
- var c = this.get(i++);
158
- if (c < 0x80) // 0xxxxxxx (7 bit)
159
- s += String.fromCharCode(c);
160
- else if (c < 0xC0)
161
- throw new Error('Invalid UTF-8 starting byte: ' + c);
162
- else if (c < 0xE0) // 110xxxxx 10xxxxxx (11 bit)
163
- s += String.fromCharCode(((c & 0x1F) << 6) | ex(this.get(i++)));
164
- else if (c < 0xF0) // 1110xxxx 10xxxxxx 10xxxxxx (16 bit)
165
- s += String.fromCharCode(((c & 0x0F) << 12) | (ex(this.get(i++)) << 6) | ex(this.get(i++)));
166
- else if (c < 0xF8) // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx (21 bit)
167
- s += surrogate(((c & 0x07) << 18) | (ex(this.get(i++)) << 12) | (ex(this.get(i++)) << 6) | ex(this.get(i++)));
168
- else
169
- throw new Error('Invalid UTF-8 starting byte (since 2003 it is restricted to 4 bytes): ' + c);
170
- }
171
- return { size: s.length, str: stringCut(s, maxLength) };
172
- };
173
- Stream.prototype.parseStringBMP = function (start, end, maxLength) {
174
- var s = "", hi, lo;
175
- for (var i = start; i < end; ) {
176
- hi = this.get(i++);
177
- lo = this.get(i++);
178
- s += String.fromCharCode((hi << 8) | lo);
179
- }
180
- return { size: s.length, str: stringCut(s, maxLength) };
181
- };
182
- Stream.prototype.parseTime = function (start, end, shortYear) {
183
- var s = this.parseStringISO(start, end).str,
184
- m = (shortYear ? reTimeS : reTimeL).exec(s);
185
- if (!m)
186
- return "Unrecognized time: " + s;
187
- if (shortYear) {
188
- // to avoid querying the timer, use the fixed range [1970, 2069]
189
- // it will conform with ITU X.400 [-10, +40] sliding window until 2030
190
- m[1] = +m[1];
191
- m[1] += (m[1] < 70) ? 2000 : 1900;
192
- }
193
- s = m[1] + "-" + m[2] + "-" + m[3] + " " + m[4];
194
- if (m[5]) {
195
- s += ":" + m[5];
196
- if (m[6]) {
197
- s += ":" + m[6];
198
- if (m[7])
199
- s += "." + m[7];
200
- }
201
- }
202
- if (m[8]) {
203
- s += " UTC";
204
- if (m[9])
205
- s += m[9] + ":" + (m[10] || "00");
206
- }
207
- return s;
208
- };
209
- Stream.prototype.parseInteger = function (start, end) {
210
- var v = this.get(start),
211
- neg = (v > 127),
212
- pad = neg ? 255 : 0,
213
- len,
214
- s = '';
215
- // skip unuseful bits (not allowed in DER)
216
- while (v == pad && ++start < end)
217
- v = this.get(start);
218
- len = end - start;
219
- if (len === 0)
220
- return neg ? '-1' : '0';
221
- // show bit length of huge integers
222
- if (len > 4) {
223
- s = v;
224
- len <<= 3;
225
- while (((s ^ pad) & 0x80) == 0) {
226
- s <<= 1;
227
- --len;
228
- }
229
- s = "(" + len + " bit)\n";
230
- }
231
- // decode the integer
232
- if (neg) v = v - 256;
233
- var n = new Int10(v);
234
- for (var i = start + 1; i < end; ++i)
235
- n.mulAdd(256, this.get(i));
236
- return s + n.toString();
237
- };
238
- Stream.prototype.parseBitString = function (start, end, maxLength) {
239
- var unusedBits = this.get(start);
240
- if (unusedBits > 7)
241
- throw 'Invalid BitString with unusedBits=' + unusedBits;
242
- var lenBit = ((end - start - 1) << 3) - unusedBits,
243
- s = "";
244
- for (var i = start + 1; i < end; ++i) {
245
- var b = this.get(i),
246
- skip = (i == end - 1) ? unusedBits : 0;
247
- for (var j = 7; j >= skip; --j)
248
- s += (b >> j) & 1 ? "1" : "0";
249
- if (s.length > maxLength)
250
- s = stringCut(s, maxLength);
251
- }
252
- return { size: lenBit, str: s };
253
- };
254
50
  function checkPrintable(s) {
255
- var i, v;
51
+ let i, v;
256
52
  for (i = 0; i < s.length; ++i) {
257
53
  v = s.charCodeAt(i);
258
54
  if (v < 32 && v != 9 && v != 10 && v != 13) // [\t\r\n] are (kinda) printable
259
- throw new Error('Unprintable character at index ' + i + ' (code ' + s.str.charCodeAt(i) + ")");
55
+ throw new Error('Unprintable character at index ' + i + ' (code ' + s.str.charCodeAt(i) + ')');
260
56
  }
261
57
  }
262
- Stream.prototype.parseOctetString = function (start, end, maxLength) {
263
- var len = end - start,
264
- s;
265
- try {
266
- s = this.parseStringUTF(start, end, maxLength);
267
- checkPrintable(s.str);
268
- return { size: end - start, str: s.str };
269
- } catch (e) {
270
- // ignore
271
- }
272
- maxLength /= 2; // we work in bytes
273
- if (len > maxLength)
274
- end = start + maxLength;
275
- s = '';
276
- for (var i = start; i < end; ++i)
277
- s += this.hexByte(this.get(i));
278
- if (len > maxLength)
279
- s += ellipsis;
280
- return { size: len, str: s };
281
- };
282
- Stream.prototype.parseOID = function (start, end, maxLength, isRelative) {
283
- var s = '',
284
- n = new Int10(),
285
- bits = 0;
286
- for (var i = start; i < end; ++i) {
287
- var v = this.get(i);
288
- n.mulAdd(128, v & 0x7F);
289
- bits += 7;
290
- if (!(v & 0x80)) { // finished
291
- if (s === '') {
292
- n = n.simplify();
293
- if (isRelative) {
294
- s = (n instanceof Int10) ? n.toString() : "" + n;
295
- } else if (n instanceof Int10) {
296
- n.sub(80);
297
- s = "2." + n.toString();
298
- } else {
299
- var m = n < 80 ? n < 40 ? 0 : 1 : 2;
300
- s = m + "." + (n - m * 40);
58
+
59
+ class Stream {
60
+
61
+ constructor(enc, pos) {
62
+ if (enc instanceof Stream) {
63
+ this.enc = enc.enc;
64
+ this.pos = enc.pos;
65
+ } else {
66
+ // enc should be an array or a binary string
67
+ this.enc = enc;
68
+ this.pos = pos;
69
+ }
70
+ }
71
+ get(pos) {
72
+ if (pos === undefined)
73
+ pos = this.pos++;
74
+ if (pos >= this.enc.length)
75
+ throw 'Requesting byte offset ' + pos + ' on a stream of length ' + this.enc.length;
76
+ return (typeof this.enc == 'string') ? this.enc.charCodeAt(pos) : this.enc[pos];
77
+ }
78
+ hexByte(b) {
79
+ return hexDigits.charAt((b >> 4) & 0xF) + hexDigits.charAt(b & 0xF);
80
+ }
81
+ hexDump(start, end, raw) {
82
+ let s = '';
83
+ for (let i = start; i < end; ++i) {
84
+ s += this.hexByte(this.get(i));
85
+ if (raw !== true)
86
+ switch (i & 0xF) {
87
+ case 0x7: s += ' '; break;
88
+ case 0xF: s += '\n'; break;
89
+ default: s += ' ';
301
90
  }
302
- } else
303
- s += "." + n.toString();
91
+ }
92
+ return s;
93
+ }
94
+ b64Dump(start, end) {
95
+ let extra = (end - start) % 3,
96
+ s = '',
97
+ i, c;
98
+ for (i = start; i + 2 < end; i += 3) {
99
+ c = this.get(i) << 16 | this.get(i + 1) << 8 | this.get(i + 2);
100
+ s += b64Safe.charAt(c >> 18 & 0x3F);
101
+ s += b64Safe.charAt(c >> 12 & 0x3F);
102
+ s += b64Safe.charAt(c >> 6 & 0x3F);
103
+ s += b64Safe.charAt(c & 0x3F);
104
+ }
105
+ if (extra > 0) {
106
+ c = this.get(i) << 16;
107
+ if (extra > 1) c |= this.get(i + 1) << 8;
108
+ s += b64Safe.charAt(c >> 18 & 0x3F);
109
+ s += b64Safe.charAt(c >> 12 & 0x3F);
110
+ if (extra == 2) s += b64Safe.charAt(c >> 6 & 0x3F);
111
+ }
112
+ return s;
113
+ }
114
+ isASCII(start, end) {
115
+ for (let i = start; i < end; ++i) {
116
+ let c = this.get(i);
117
+ if (c < 32 || c > 176)
118
+ return false;
119
+ }
120
+ return true;
121
+ }
122
+ parseStringISO(start, end, maxLength) {
123
+ let s = '';
124
+ for (let i = start; i < end; ++i)
125
+ s += String.fromCharCode(this.get(i));
126
+ return { size: s.length, str: stringCut(s, maxLength) };
127
+ }
128
+ parseStringT61(start, end, maxLength) {
129
+ // warning: this code is not very well tested so far
130
+ function merge(c, d) {
131
+ let t = tableT61[c - 0xC0];
132
+ let i = t[0].indexOf(String.fromCharCode(d));
133
+ return (i < 0) ? '\0' : t[1].charAt(i);
134
+ }
135
+ let s = '', c;
136
+ for (let i = start; i < end; ++i) {
137
+ c = this.get(i);
138
+ if (c >= 0xA4 && c <= 0xBF)
139
+ s += '$¥#§¤\0\0«\0\0\0\0°±²³×µ¶·÷\0\0»¼½¾¿'.charAt(c - 0xA4);
140
+ else if (c >= 0xE0 && c <= 0xFF)
141
+ s += 'ΩÆÐªĦ\0IJĿŁØŒºÞŦŊʼnĸæđðħıijŀłøœßþŧŋ\0'.charAt(c - 0xE0);
142
+ else if (c >= 0xC0 && c <= 0xCF)
143
+ s += merge(c, this.get(++i));
144
+ else // using ISO 8859-1 for characters undefined (or equal) in T.61
145
+ s += String.fromCharCode(c);
146
+ }
147
+ return { size: s.length, str: stringCut(s, maxLength) };
148
+ }
149
+ parseStringUTF(start, end, maxLength) {
150
+ function ex(c) { // must be 10xxxxxx
151
+ if ((c < 0x80) || (c >= 0xC0))
152
+ throw new Error('Invalid UTF-8 continuation byte: ' + c);
153
+ return (c & 0x3F);
154
+ }
155
+ function surrogate(cp) {
156
+ if (cp < 0x10000)
157
+ throw new Error('UTF-8 overlong encoding, codepoint encoded in 4 bytes: ' + cp);
158
+ // we could use String.fromCodePoint(cp) but let's be nice to older browsers and use surrogate pairs
159
+ cp -= 0x10000;
160
+ return String.fromCharCode((cp >> 10) + 0xD800, (cp & 0x3FF) + 0xDC00);
161
+ }
162
+ let s = '';
163
+ for (let i = start; i < end; ) {
164
+ let c = this.get(i++);
165
+ if (c < 0x80) // 0xxxxxxx (7 bit)
166
+ s += String.fromCharCode(c);
167
+ else if (c < 0xC0)
168
+ throw new Error('Invalid UTF-8 starting byte: ' + c);
169
+ else if (c < 0xE0) // 110xxxxx 10xxxxxx (11 bit)
170
+ s += String.fromCharCode(((c & 0x1F) << 6) | ex(this.get(i++)));
171
+ else if (c < 0xF0) // 1110xxxx 10xxxxxx 10xxxxxx (16 bit)
172
+ s += String.fromCharCode(((c & 0x0F) << 12) | (ex(this.get(i++)) << 6) | ex(this.get(i++)));
173
+ else if (c < 0xF8) // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx (21 bit)
174
+ s += surrogate(((c & 0x07) << 18) | (ex(this.get(i++)) << 12) | (ex(this.get(i++)) << 6) | ex(this.get(i++)));
175
+ else
176
+ throw new Error('Invalid UTF-8 starting byte (since 2003 it is restricted to 4 bytes): ' + c);
177
+ }
178
+ return { size: s.length, str: stringCut(s, maxLength) };
179
+ }
180
+ parseStringBMP(start, end, maxLength) {
181
+ let s = '', hi, lo;
182
+ for (let i = start; i < end; ) {
183
+ hi = this.get(i++);
184
+ lo = this.get(i++);
185
+ s += String.fromCharCode((hi << 8) | lo);
186
+ }
187
+ return { size: s.length, str: stringCut(s, maxLength) };
188
+ }
189
+ parseTime(start, end, shortYear) {
190
+ let s = this.parseStringISO(start, end).str,
191
+ m = (shortYear ? reTimeS : reTimeL).exec(s);
192
+ if (!m)
193
+ return 'Unrecognized time: ' + s;
194
+ if (shortYear) {
195
+ // to avoid querying the timer, use the fixed range [1970, 2069]
196
+ // it will conform with ITU X.400 [-10, +40] sliding window until 2030
197
+ m[1] = +m[1];
198
+ m[1] += (m[1] < 70) ? 2000 : 1900;
199
+ }
200
+ s = m[1] + '-' + m[2] + '-' + m[3] + ' ' + m[4];
201
+ if (m[5]) {
202
+ s += ':' + m[5];
203
+ if (m[6]) {
204
+ s += ':' + m[6];
205
+ if (m[7])
206
+ s += '.' + m[7];
207
+ }
208
+ }
209
+ if (m[8]) {
210
+ s += ' UTC';
211
+ if (m[9])
212
+ s += m[9] + ':' + (m[10] || '00');
213
+ }
214
+ return s;
215
+ }
216
+ parseInteger(start, end) {
217
+ let v = this.get(start),
218
+ neg = (v > 127),
219
+ pad = neg ? 255 : 0,
220
+ len,
221
+ s = '';
222
+ // skip unuseful bits (not allowed in DER)
223
+ while (v == pad && ++start < end)
224
+ v = this.get(start);
225
+ len = end - start;
226
+ if (len === 0)
227
+ return neg ? '-1' : '0';
228
+ // show bit length of huge integers
229
+ if (len > 4) {
230
+ s = v;
231
+ len <<= 3;
232
+ while (((s ^ pad) & 0x80) == 0) {
233
+ s <<= 1;
234
+ --len;
235
+ }
236
+ s = '(' + len + ' bit)\n';
237
+ }
238
+ // decode the integer
239
+ if (neg) v = v - 256;
240
+ let n = new Int10(v);
241
+ for (let i = start + 1; i < end; ++i)
242
+ n.mulAdd(256, this.get(i));
243
+ return s + n.toString();
244
+ }
245
+ parseBitString(start, end, maxLength) {
246
+ let unusedBits = this.get(start);
247
+ if (unusedBits > 7)
248
+ throw 'Invalid BitString with unusedBits=' + unusedBits;
249
+ let lenBit = ((end - start - 1) << 3) - unusedBits,
250
+ s = '';
251
+ for (let i = start + 1; i < end; ++i) {
252
+ let b = this.get(i),
253
+ skip = (i == end - 1) ? unusedBits : 0;
254
+ for (let j = 7; j >= skip; --j)
255
+ s += (b >> j) & 1 ? '1' : '0';
304
256
  if (s.length > maxLength)
305
- return stringCut(s, maxLength);
306
- n = new Int10();
307
- bits = 0;
257
+ s = stringCut(s, maxLength);
308
258
  }
259
+ return { size: lenBit, str: s };
309
260
  }
310
- if (bits > 0)
311
- s += ".incomplete";
312
- if (typeof oids === 'object' && !isRelative) {
313
- var oid = oids[s];
314
- if (oid) {
315
- if (oid.d) s += "\n" + oid.d;
316
- if (oid.c) s += "\n" + oid.c;
317
- if (oid.w) s += "\n(warning!)";
261
+ parseOctetString(start, end, maxLength) {
262
+ let len = end - start,
263
+ s;
264
+ try {
265
+ s = this.parseStringUTF(start, end, maxLength);
266
+ checkPrintable(s.str);
267
+ return { size: end - start, str: s.str };
268
+ } catch (e) {
269
+ // ignore
318
270
  }
271
+ maxLength /= 2; // we work in bytes
272
+ if (len > maxLength)
273
+ end = start + maxLength;
274
+ s = '';
275
+ for (let i = start; i < end; ++i)
276
+ s += this.hexByte(this.get(i));
277
+ if (len > maxLength)
278
+ s += ellipsis;
279
+ return { size: len, str: s };
319
280
  }
320
- return s;
321
- };
322
- Stream.prototype.parseRelativeOID = function (start, end, maxLength) {
323
- return this.parseOID(start, end, maxLength, true);
324
- };
325
-
326
- function ASN1(stream, header, length, tag, tagLen, sub) {
327
- if (!(tag instanceof ASN1Tag)) throw 'Invalid tag value.';
328
- this.stream = stream;
329
- this.header = header;
330
- this.length = length;
331
- this.tag = tag;
332
- this.tagLen = tagLen;
333
- this.sub = sub;
334
- }
335
- ASN1.prototype.typeName = function () {
336
- switch (this.tag.tagClass) {
337
- case 0: // universal
338
- switch (this.tag.tagNumber) {
339
- case 0x00: return "EOC";
340
- case 0x01: return "BOOLEAN";
341
- case 0x02: return "INTEGER";
342
- case 0x03: return "BIT_STRING";
343
- case 0x04: return "OCTET_STRING";
344
- case 0x05: return "NULL";
345
- case 0x06: return "OBJECT_IDENTIFIER";
346
- case 0x07: return "ObjectDescriptor";
347
- case 0x08: return "EXTERNAL";
348
- case 0x09: return "REAL";
349
- case 0x0A: return "ENUMERATED";
350
- case 0x0B: return "EMBEDDED_PDV";
351
- case 0x0C: return "UTF8String";
352
- case 0x0D: return "RELATIVE_OID";
353
- case 0x10: return "SEQUENCE";
354
- case 0x11: return "SET";
355
- case 0x12: return "NumericString";
356
- case 0x13: return "PrintableString"; // ASCII subset
357
- case 0x14: return "TeletexString"; // aka T61String
358
- case 0x15: return "VideotexString";
359
- case 0x16: return "IA5String"; // ASCII
360
- case 0x17: return "UTCTime";
361
- case 0x18: return "GeneralizedTime";
362
- case 0x19: return "GraphicString";
363
- case 0x1A: return "VisibleString"; // ASCII subset
364
- case 0x1B: return "GeneralString";
365
- case 0x1C: return "UniversalString";
366
- case 0x1E: return "BMPString";
281
+ parseOID(start, end, maxLength, isRelative) {
282
+ let s = '',
283
+ n = new Int10(),
284
+ bits = 0;
285
+ for (let i = start; i < end; ++i) {
286
+ let v = this.get(i);
287
+ n.mulAdd(128, v & 0x7F);
288
+ bits += 7;
289
+ if (!(v & 0x80)) { // finished
290
+ if (s === '') {
291
+ n = n.simplify();
292
+ if (isRelative) {
293
+ s = (n instanceof Int10) ? n.toString() : '' + n;
294
+ } else if (n instanceof Int10) {
295
+ n.sub(80);
296
+ s = '2.' + n.toString();
297
+ } else {
298
+ let m = n < 80 ? n < 40 ? 0 : 1 : 2;
299
+ s = m + '.' + (n - m * 40);
300
+ }
301
+ } else
302
+ s += '.' + n.toString();
303
+ if (s.length > maxLength)
304
+ return stringCut(s, maxLength);
305
+ n = new Int10();
306
+ bits = 0;
307
+ }
308
+ }
309
+ if (bits > 0)
310
+ s += '.incomplete';
311
+ if (typeof oids === 'object' && !isRelative) {
312
+ let oid = oids[s];
313
+ if (oid) {
314
+ if (oid.d) s += '\n' + oid.d;
315
+ if (oid.c) s += '\n' + oid.c;
316
+ if (oid.w) s += '\n(warning!)';
317
+ }
367
318
  }
368
- return "Universal_" + this.tag.tagNumber.toString();
369
- case 1: return "Application_" + this.tag.tagNumber.toString();
370
- case 2: return "[" + this.tag.tagNumber.toString() + "]"; // Context
371
- case 3: return "Private_" + this.tag.tagNumber.toString();
319
+ return s;
372
320
  }
373
- };
321
+ parseRelativeOID(start, end, maxLength) {
322
+ return this.parseOID(start, end, maxLength, true);
323
+ }
324
+ }
325
+
374
326
  function recurse(el, parser, maxLength) {
375
- var avoidRecurse = true;
327
+ let avoidRecurse = true;
376
328
  if (el.tag.tagConstructed && el.sub) {
377
329
  avoidRecurse = false;
378
330
  el.sub.forEach(function (e1) {
@@ -382,217 +334,272 @@ function recurse(el, parser, maxLength) {
382
334
  }
383
335
  if (avoidRecurse)
384
336
  return el.stream[parser](el.posContent(), el.posContent() + Math.abs(el.length), maxLength);
385
- var d = { size: 0, str: '' };
337
+ let d = { size: 0, str: '' };
386
338
  el.sub.forEach(function (el) {
387
- var d1 = recurse(el, parser, maxLength - d.str.length);
339
+ let d1 = recurse(el, parser, maxLength - d.str.length);
388
340
  d.size += d1.size;
389
341
  d.str += d1.str;
390
342
  });
391
343
  return d;
392
344
  }
393
- /** A string preview of the content (intended for humans). */
394
- ASN1.prototype.content = function (maxLength) {
395
- if (this.tag === undefined)
396
- return null;
397
- if (maxLength === undefined)
398
- maxLength = Infinity;
399
- var content = this.posContent(),
400
- len = Math.abs(this.length);
401
- if (!this.tag.isUniversal()) {
402
- if (this.sub !== null)
403
- return "(" + this.sub.length + " elem)";
404
- var d1 = this.stream.parseOctetString(content, content + len, maxLength);
405
- return "(" + d1.size + " byte)\n" + d1.str;
406
- }
407
- switch (this.tag.tagNumber) {
408
- case 0x01: // BOOLEAN
409
- return (this.stream.get(content) === 0) ? "false" : "true";
410
- case 0x02: // INTEGER
411
- return this.stream.parseInteger(content, content + len);
412
- case 0x03: // BIT_STRING
413
- var d = recurse(this, 'parseBitString', maxLength);
414
- return "(" + d.size + " bit)\n" + d.str;
415
- case 0x04: // OCTET_STRING
416
- d = recurse(this, 'parseOctetString', maxLength);
417
- return "(" + d.size + " byte)\n" + d.str;
418
- //case 0x05: // NULL
419
- case 0x06: // OBJECT_IDENTIFIER
420
- return this.stream.parseOID(content, content + len, maxLength);
421
- //case 0x07: // ObjectDescriptor
422
- //case 0x08: // EXTERNAL
423
- //case 0x09: // REAL
424
- case 0x0A: // ENUMERATED
425
- return this.stream.parseInteger(content, content + len);
426
- //case 0x0B: // EMBEDDED_PDV
427
- case 0x0D: // RELATIVE-OID
428
- return this.stream.parseRelativeOID(content, content + len, maxLength);
429
- case 0x10: // SEQUENCE
430
- case 0x11: // SET
431
- if (this.sub !== null)
432
- return "(" + this.sub.length + " elem)";
433
- else
434
- return "(no elem)";
435
- case 0x0C: // UTF8String
436
- return recurse(this, 'parseStringUTF', maxLength).str;
437
- case 0x14: // TeletexString
438
- return recurse(this, 'parseStringT61', maxLength).str;
439
- case 0x12: // NumericString
440
- case 0x13: // PrintableString
441
- case 0x15: // VideotexString
442
- case 0x16: // IA5String
443
- case 0x1A: // VisibleString
444
- case 0x1B: // GeneralString
445
- //case 0x19: // GraphicString
446
- //case 0x1C: // UniversalString
447
- return recurse(this, 'parseStringISO', maxLength).str;
448
- case 0x1E: // BMPString
449
- return recurse(this, 'parseStringBMP', maxLength).str;
450
- case 0x17: // UTCTime
451
- case 0x18: // GeneralizedTime
452
- return this.stream.parseTime(content, content + len, (this.tag.tagNumber == 0x17));
453
- }
454
- return null;
455
- };
456
- ASN1.prototype.toString = function () {
457
- return this.typeName() + "@" + this.stream.pos + "[header:" + this.header + ",length:" + this.length + ",sub:" + ((this.sub === null) ? 'null' : this.sub.length) + "]";
458
- };
459
- ASN1.prototype.toPrettyString = function (indent) {
460
- if (indent === undefined) indent = '';
461
- var s = indent + this.typeName() + " @" + this.stream.pos;
462
- if (this.length >= 0)
463
- s += "+";
464
- s += this.length;
465
- if (this.tag.tagConstructed)
466
- s += " (constructed)";
467
- else if ((this.tag.isUniversal() && ((this.tag.tagNumber == 0x03) || (this.tag.tagNumber == 0x04))) && (this.sub !== null))
468
- s += " (encapsulates)";
469
- var content = this.content();
470
- if (content)
471
- s += ": " + content.replace(/\n/g, '|');
472
- s += "\n";
473
- if (this.sub !== null) {
474
- indent += ' ';
475
- for (var i = 0, max = this.sub.length; i < max; ++i)
476
- s += this.sub[i].toPrettyString(indent);
477
- }
478
- return s;
479
- };
480
- ASN1.prototype.posStart = function () {
481
- return this.stream.pos;
482
- };
483
- ASN1.prototype.posContent = function () {
484
- return this.stream.pos + this.header;
485
- };
486
- ASN1.prototype.posEnd = function () {
487
- return this.stream.pos + this.header + Math.abs(this.length);
488
- };
489
- /** Position of the length. */
490
- ASN1.prototype.posLen = function() {
491
- return this.stream.pos + this.tagLen;
492
- };
493
- ASN1.prototype.toHexString = function () {
494
- return this.stream.hexDump(this.posStart(), this.posEnd(), true);
495
- };
496
- ASN1.prototype.toB64String = function () {
497
- return this.stream.b64Dump(this.posStart(), this.posEnd());
498
- };
499
- ASN1.decodeLength = function (stream) {
500
- var buf = stream.get(),
501
- len = buf & 0x7F;
502
- if (len == buf) // first bit was 0, short form
503
- return len;
504
- if (len === 0) // long form with length 0 is a special case
505
- return null; // undefined length
506
- if (len > 6) // no reason to use Int10, as it would be a huge buffer anyways
507
- throw "Length over 48 bits not supported at position " + (stream.pos - 1);
508
- buf = 0;
509
- for (var i = 0; i < len; ++i)
510
- buf = (buf * 256) + stream.get();
511
- return buf;
512
- };
513
- function ASN1Tag(stream) {
514
- var buf = stream.get();
515
- this.tagClass = buf >> 6;
516
- this.tagConstructed = ((buf & 0x20) !== 0);
517
- this.tagNumber = buf & 0x1F;
518
- if (this.tagNumber == 0x1F) { // long tag
519
- var n = new Int10();
520
- do {
521
- buf = stream.get();
522
- n.mulAdd(128, buf & 0x7F);
523
- } while (buf & 0x80);
524
- this.tagNumber = n.simplify();
345
+
346
+ class ASN1Tag {
347
+ constructor(stream) {
348
+ let buf = stream.get();
349
+ this.tagClass = buf >> 6;
350
+ this.tagConstructed = ((buf & 0x20) !== 0);
351
+ this.tagNumber = buf & 0x1F;
352
+ if (this.tagNumber == 0x1F) { // long tag
353
+ let n = new Int10();
354
+ do {
355
+ buf = stream.get();
356
+ n.mulAdd(128, buf & 0x7F);
357
+ } while (buf & 0x80);
358
+ this.tagNumber = n.simplify();
359
+ }
360
+ }
361
+ isUniversal() {
362
+ return this.tagClass === 0x00;
363
+ }
364
+ isEOC() {
365
+ return this.tagClass === 0x00 && this.tagNumber === 0x00;
525
366
  }
526
367
  }
527
- ASN1Tag.prototype.isUniversal = function () {
528
- return this.tagClass === 0x00;
529
- };
530
- ASN1Tag.prototype.isEOC = function () {
531
- return this.tagClass === 0x00 && this.tagNumber === 0x00;
532
- };
533
- ASN1.decode = function (stream, offset) {
534
- if (!(stream instanceof Stream))
535
- stream = new Stream(stream, offset || 0);
536
- var streamStart = new Stream(stream),
537
- tag = new ASN1Tag(stream),
538
- tagLen = stream.pos - streamStart.pos,
539
- len = ASN1.decodeLength(stream),
540
- start = stream.pos,
541
- header = start - streamStart.pos,
542
- sub = null,
543
- getSub = function () {
544
- sub = [];
545
- if (len !== null) {
546
- // definite length
547
- var end = start + len;
548
- if (end > stream.enc.length)
549
- throw 'Container at offset ' + start + ' has a length of ' + len + ', which is past the end of the stream';
550
- while (stream.pos < end)
551
- sub[sub.length] = ASN1.decode(stream);
552
- if (stream.pos != end)
553
- throw 'Content size is not correct for container at offset ' + start;
554
- } else {
555
- // undefined length
556
- try {
557
- for (;;) {
558
- var s = ASN1.decode(stream);
559
- if (s.tag.isEOC())
560
- break;
561
- sub[sub.length] = s;
368
+
369
+ export class ASN1 {
370
+ constructor(stream, header, length, tag, tagLen, sub) {
371
+ if (!(tag instanceof ASN1Tag)) throw 'Invalid tag value.';
372
+ this.stream = stream;
373
+ this.header = header;
374
+ this.length = length;
375
+ this.tag = tag;
376
+ this.tagLen = tagLen;
377
+ this.sub = sub;
378
+ }
379
+ typeName() {
380
+ switch (this.tag.tagClass) {
381
+ case 0: // universal
382
+ switch (this.tag.tagNumber) {
383
+ case 0x00: return 'EOC';
384
+ case 0x01: return 'BOOLEAN';
385
+ case 0x02: return 'INTEGER';
386
+ case 0x03: return 'BIT_STRING';
387
+ case 0x04: return 'OCTET_STRING';
388
+ case 0x05: return 'NULL';
389
+ case 0x06: return 'OBJECT_IDENTIFIER';
390
+ case 0x07: return 'ObjectDescriptor';
391
+ case 0x08: return 'EXTERNAL';
392
+ case 0x09: return 'REAL';
393
+ case 0x0A: return 'ENUMERATED';
394
+ case 0x0B: return 'EMBEDDED_PDV';
395
+ case 0x0C: return 'UTF8String';
396
+ case 0x0D: return 'RELATIVE_OID';
397
+ case 0x10: return 'SEQUENCE';
398
+ case 0x11: return 'SET';
399
+ case 0x12: return 'NumericString';
400
+ case 0x13: return 'PrintableString'; // ASCII subset
401
+ case 0x14: return 'TeletexString'; // aka T61String
402
+ case 0x15: return 'VideotexString';
403
+ case 0x16: return 'IA5String'; // ASCII
404
+ case 0x17: return 'UTCTime';
405
+ case 0x18: return 'GeneralizedTime';
406
+ case 0x19: return 'GraphicString';
407
+ case 0x1A: return 'VisibleString'; // ASCII subset
408
+ case 0x1B: return 'GeneralString';
409
+ case 0x1C: return 'UniversalString';
410
+ case 0x1E: return 'BMPString';
411
+ }
412
+ return 'Universal_' + this.tag.tagNumber.toString();
413
+ case 1: return 'Application_' + this.tag.tagNumber.toString();
414
+ case 2: return '[' + this.tag.tagNumber.toString() + ']'; // Context
415
+ case 3: return 'Private_' + this.tag.tagNumber.toString();
416
+ }
417
+ }
418
+ /** A string preview of the content (intended for humans). */
419
+ content(maxLength) {
420
+ if (this.tag === undefined)
421
+ return null;
422
+ if (maxLength === undefined)
423
+ maxLength = Infinity;
424
+ let content = this.posContent(),
425
+ len = Math.abs(this.length);
426
+ if (!this.tag.isUniversal()) {
427
+ if (this.sub !== null)
428
+ return '(' + this.sub.length + ' elem)';
429
+ let d1 = this.stream.parseOctetString(content, content + len, maxLength);
430
+ return '(' + d1.size + ' byte)\n' + d1.str;
431
+ }
432
+ switch (this.tag.tagNumber) {
433
+ case 0x01: // BOOLEAN
434
+ return (this.stream.get(content) === 0) ? 'false' : 'true';
435
+ case 0x02: // INTEGER
436
+ return this.stream.parseInteger(content, content + len);
437
+ case 0x03: { // BIT_STRING
438
+ let d = recurse(this, 'parseBitString', maxLength);
439
+ return '(' + d.size + ' bit)\n' + d.str;
440
+ }
441
+ case 0x04: { // OCTET_STRING
442
+ let d = recurse(this, 'parseOctetString', maxLength);
443
+ return '(' + d.size + ' byte)\n' + d.str;
444
+ }
445
+ //case 0x05: // NULL
446
+ case 0x06: // OBJECT_IDENTIFIER
447
+ return this.stream.parseOID(content, content + len, maxLength);
448
+ //case 0x07: // ObjectDescriptor
449
+ //case 0x08: // EXTERNAL
450
+ //case 0x09: // REAL
451
+ case 0x0A: // ENUMERATED
452
+ return this.stream.parseInteger(content, content + len);
453
+ //case 0x0B: // EMBEDDED_PDV
454
+ case 0x0D: // RELATIVE-OID
455
+ return this.stream.parseRelativeOID(content, content + len, maxLength);
456
+ case 0x10: // SEQUENCE
457
+ case 0x11: // SET
458
+ if (this.sub !== null)
459
+ return '(' + this.sub.length + ' elem)';
460
+ else
461
+ return '(no elem)';
462
+ case 0x0C: // UTF8String
463
+ return recurse(this, 'parseStringUTF', maxLength).str;
464
+ case 0x14: // TeletexString
465
+ return recurse(this, 'parseStringT61', maxLength).str;
466
+ case 0x12: // NumericString
467
+ case 0x13: // PrintableString
468
+ case 0x15: // VideotexString
469
+ case 0x16: // IA5String
470
+ case 0x1A: // VisibleString
471
+ case 0x1B: // GeneralString
472
+ //case 0x19: // GraphicString
473
+ //case 0x1C: // UniversalString
474
+ return recurse(this, 'parseStringISO', maxLength).str;
475
+ case 0x1E: // BMPString
476
+ return recurse(this, 'parseStringBMP', maxLength).str;
477
+ case 0x17: // UTCTime
478
+ case 0x18: // GeneralizedTime
479
+ return this.stream.parseTime(content, content + len, (this.tag.tagNumber == 0x17));
480
+ }
481
+ return null;
482
+ }
483
+ toString() {
484
+ return this.typeName() + '@' + this.stream.pos + '[header:' + this.header + ',length:' + this.length + ',sub:' + ((this.sub === null) ? 'null' : this.sub.length) + ']';
485
+ }
486
+ toPrettyString(indent) {
487
+ if (indent === undefined) indent = '';
488
+ let s = indent + this.typeName() + ' @' + this.stream.pos;
489
+ if (this.length >= 0)
490
+ s += '+';
491
+ s += this.length;
492
+ if (this.tag.tagConstructed)
493
+ s += ' (constructed)';
494
+ else if ((this.tag.isUniversal() && ((this.tag.tagNumber == 0x03) || (this.tag.tagNumber == 0x04))) && (this.sub !== null))
495
+ s += ' (encapsulates)';
496
+ let content = this.content();
497
+ if (content)
498
+ s += ': ' + content.replace(/\n/g, '|');
499
+ s += '\n';
500
+ if (this.sub !== null) {
501
+ indent += ' ';
502
+ for (let i = 0, max = this.sub.length; i < max; ++i)
503
+ s += this.sub[i].toPrettyString(indent);
504
+ }
505
+ return s;
506
+ }
507
+ posStart() {
508
+ return this.stream.pos;
509
+ }
510
+ posContent() {
511
+ return this.stream.pos + this.header;
512
+ }
513
+ posEnd() {
514
+ return this.stream.pos + this.header + Math.abs(this.length);
515
+ }
516
+ /** Position of the length. */
517
+ posLen() {
518
+ return this.stream.pos + this.tagLen;
519
+ }
520
+ toHexString() {
521
+ return this.stream.hexDump(this.posStart(), this.posEnd(), true);
522
+ }
523
+ toB64String() {
524
+ return this.stream.b64Dump(this.posStart(), this.posEnd());
525
+ }
526
+ static decodeLength(stream) {
527
+ let buf = stream.get(),
528
+ len = buf & 0x7F;
529
+ if (len == buf) // first bit was 0, short form
530
+ return len;
531
+ if (len === 0) // long form with length 0 is a special case
532
+ return null; // undefined length
533
+ if (len > 6) // no reason to use Int10, as it would be a huge buffer anyways
534
+ throw 'Length over 48 bits not supported at position ' + (stream.pos - 1);
535
+ buf = 0;
536
+ for (let i = 0; i < len; ++i)
537
+ buf = (buf * 256) + stream.get();
538
+ return buf;
539
+ }
540
+ static decode(stream, offset, type = ASN1) {
541
+ if (!(type == ASN1 || type.prototype instanceof ASN1))
542
+ throw 'Must pass a class that extends ASN1';
543
+ if (!(stream instanceof Stream))
544
+ stream = new Stream(stream, offset || 0);
545
+ let streamStart = new Stream(stream),
546
+ tag = new ASN1Tag(stream),
547
+ tagLen = stream.pos - streamStart.pos,
548
+ len = ASN1.decodeLength(stream),
549
+ start = stream.pos,
550
+ header = start - streamStart.pos,
551
+ sub = null,
552
+ getSub = function () {
553
+ sub = [];
554
+ if (len !== null) {
555
+ // definite length
556
+ let end = start + len;
557
+ if (end > stream.enc.length)
558
+ throw 'Container at offset ' + start + ' has a length of ' + len + ', which is past the end of the stream';
559
+ while (stream.pos < end)
560
+ sub[sub.length] = type.decode(stream);
561
+ if (stream.pos != end)
562
+ throw 'Content size is not correct for container at offset ' + start;
563
+ } else {
564
+ // undefined length
565
+ try {
566
+ for (;;) {
567
+ let s = type.decode(stream);
568
+ if (s.tag.isEOC())
569
+ break;
570
+ sub[sub.length] = s;
571
+ }
572
+ len = start - stream.pos; // undefined lengths are represented as negative values
573
+ } catch (e) {
574
+ throw 'Exception while decoding undefined length content at offset ' + start + ': ' + e;
562
575
  }
563
- len = start - stream.pos; // undefined lengths are represented as negative values
564
- } catch (e) {
565
- throw 'Exception while decoding undefined length content at offset ' + start + ': ' + e;
566
576
  }
567
- }
568
- };
569
- if (tag.tagConstructed) {
570
- // must have valid content
571
- getSub();
572
- } else if (tag.isUniversal() && ((tag.tagNumber == 0x03) || (tag.tagNumber == 0x04))) {
573
- // sometimes BitString and OctetString are used to encapsulate ASN.1
574
- try {
575
- if (tag.tagNumber == 0x03)
576
- if (stream.get() != 0)
577
- throw "BIT STRINGs with unused bits cannot encapsulate.";
577
+ };
578
+ if (tag.tagConstructed) {
579
+ // must have valid content
578
580
  getSub();
579
- for (var i = 0; i < sub.length; ++i)
580
- if (sub[i].tag.isEOC())
581
- throw 'EOC is not supposed to be actual content.';
582
- } catch (e) {
583
- // but silently ignore when they don't
584
- sub = null;
585
- //DEBUG console.log('Could not decode structure at ' + start + ':', e);
581
+ } else if (tag.isUniversal() && ((tag.tagNumber == 0x03) || (tag.tagNumber == 0x04))) {
582
+ // sometimes BitString and OctetString are used to encapsulate ASN.1
583
+ try {
584
+ if (tag.tagNumber == 0x03)
585
+ if (stream.get() != 0)
586
+ throw 'BIT STRINGs with unused bits cannot encapsulate.';
587
+ getSub();
588
+ for (let i = 0; i < sub.length; ++i)
589
+ if (sub[i].tag.isEOC())
590
+ throw 'EOC is not supposed to be actual content.';
591
+ } catch (e) {
592
+ // but silently ignore when they don't
593
+ sub = null;
594
+ //DEBUG console.log('Could not decode structure at ' + start + ':', e);
595
+ }
586
596
  }
597
+ if (sub === null) {
598
+ if (len === null)
599
+ throw "We can't skip over an invalid tag with undefined length at offset " + start;
600
+ stream.pos = start + Math.abs(len);
601
+ }
602
+ return new type(streamStart, header, len, tag, tagLen, sub);
587
603
  }
588
- if (sub === null) {
589
- if (len === null)
590
- throw "We can't skip over an invalid tag with undefined length at offset " + start;
591
- stream.pos = start + Math.abs(len);
592
- }
593
- return new ASN1(streamStart, header, len, tag, tagLen, sub);
594
- };
595
604
 
596
- return ASN1;
597
-
598
- });
605
+ }