@lapo/asn1js 1.2.3 → 1.2.4

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/LICENSE CHANGED
@@ -1,4 +1,6 @@
1
- ASN.1 JavaScript decoder Copyright (c) 2008-2021 Lapo Luchini <lapo@lapo.it>
1
+ ISC License
2
+
3
+ Copyright (c) 2008-2022 Lapo Luchini <lapo@lapo.it>
2
4
 
3
5
  Permission to use, copy, modify, and/or distribute this software for any
4
6
  purpose with or without fee is hereby granted, provided that the above
@@ -11,4 +13,3 @@ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
13
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12
14
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13
15
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
-
package/README.md CHANGED
@@ -52,7 +52,7 @@ require([
52
52
  ISC license
53
53
  -----------
54
54
 
55
- ASN.1 JavaScript decoder Copyright (c) 2008-2020 Lapo Luchini <lapo@lapo.it>
55
+ ASN.1 JavaScript decoder Copyright (c) 2008-2022 Lapo Luchini <lapo@lapo.it>
56
56
 
57
57
  Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
58
58
 
@@ -65,6 +65,7 @@ credits
65
65
  - BMPString support added by [Felipe Gasper](https://github.com/FGasper)
66
66
  - extended tag support added by [Péter Budai](https://www.peterbudai.eu/)
67
67
  - patches by [Gergely Nagy](https://github.com/ngg)
68
+ - Relative OID support added by [Mistial Developer](https://github.com/mistial-dev)
68
69
 
69
70
  links
70
71
  -----
package/asn1.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // ASN.1 JavaScript decoder
2
- // Copyright (c) 2008-2021 Lapo Luchini <lapo@lapo.it>
2
+ // Copyright (c) 2008-2022 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
@@ -22,8 +22,8 @@
22
22
  var Int10 = require('./int10'),
23
23
  oids = require('./oids'),
24
24
  ellipsis = "\u2026",
25
- 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-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-5]\d)?)?$/;
25
+ 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)?)?$/;
27
27
 
28
28
  function stringCut(str, len) {
29
29
  if (str.length > len)
@@ -94,13 +94,52 @@ Stream.prototype.isASCII = function (start, end) {
94
94
  }
95
95
  return true;
96
96
  };
97
- Stream.prototype.parseStringISO = function (start, end) {
97
+ Stream.prototype.parseStringISO = function (start, end, maxLength) {
98
98
  var s = "";
99
99
  for (var i = start; i < end; ++i)
100
100
  s += String.fromCharCode(this.get(i));
101
- return s;
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) };
102
141
  };
103
- Stream.prototype.parseStringUTF = function (start, end) {
142
+ Stream.prototype.parseStringUTF = function (start, end, maxLength) {
104
143
  function ex(c) { // must be 10xxxxxx
105
144
  if ((c < 0x80) || (c >= 0xC0))
106
145
  throw new Error('Invalid UTF-8 continuation byte: ' + c);
@@ -129,19 +168,19 @@ Stream.prototype.parseStringUTF = function (start, end) {
129
168
  else
130
169
  throw new Error('Invalid UTF-8 starting byte (since 2003 it is restricted to 4 bytes): ' + c);
131
170
  }
132
- return s;
171
+ return { size: s.length, str: stringCut(s, maxLength) };
133
172
  };
134
- Stream.prototype.parseStringBMP = function (start, end) {
135
- var str = "", hi, lo;
173
+ Stream.prototype.parseStringBMP = function (start, end, maxLength) {
174
+ var s = "", hi, lo;
136
175
  for (var i = start; i < end; ) {
137
176
  hi = this.get(i++);
138
177
  lo = this.get(i++);
139
- str += String.fromCharCode((hi << 8) | lo);
178
+ s += String.fromCharCode((hi << 8) | lo);
140
179
  }
141
- return str;
180
+ return { size: s.length, str: stringCut(s, maxLength) };
142
181
  };
143
182
  Stream.prototype.parseTime = function (start, end, shortYear) {
144
- var s = this.parseStringISO(start, end),
183
+ var s = this.parseStringISO(start, end).str,
145
184
  m = (shortYear ? reTimeS : reTimeL).exec(s);
146
185
  if (!m)
147
186
  return "Unrecognized time: " + s;
@@ -162,11 +201,8 @@ Stream.prototype.parseTime = function (start, end, shortYear) {
162
201
  }
163
202
  if (m[8]) {
164
203
  s += " UTC";
165
- if (m[8] != 'Z') {
166
- s += m[8];
167
- if (m[9])
168
- s += ":" + m[9];
169
- }
204
+ if (m[9])
205
+ s += m[9] + ":" + (m[10] || "00");
170
206
  }
171
207
  return s;
172
208
  };
@@ -215,18 +251,21 @@ Stream.prototype.parseBitString = function (start, end, maxLength) {
215
251
  }
216
252
  return { size: lenBit, str: s };
217
253
  };
254
+ function checkPrintable(s) {
255
+ var i, v;
256
+ for (i = 0; i < s.length; ++i) {
257
+ v = s.charCodeAt(i);
258
+ 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) + ")");
260
+ }
261
+ }
218
262
  Stream.prototype.parseOctetString = function (start, end, maxLength) {
219
263
  var len = end - start,
220
264
  s;
221
265
  try {
222
- s = this.parseStringUTF(start, end);
223
- var v;
224
- for (i = 0; i < s.length; ++i) {
225
- v = s.charCodeAt(i);
226
- if (v < 32 && v != 9 && v != 10 && v != 13) // [\t\r\n] are (kinda) printable
227
- throw new Error('Unprintable character at index ' + i + ' (code ' + s.charCodeAt(i) + ")");
228
- }
229
- return { size: len, str: s };
266
+ s = this.parseStringUTF(start, end, maxLength);
267
+ checkPrintable(s.str);
268
+ return { size: end - start, str: s.str };
230
269
  } catch (e) {
231
270
  // ignore
232
271
  }
@@ -240,7 +279,7 @@ Stream.prototype.parseOctetString = function (start, end, maxLength) {
240
279
  s += ellipsis;
241
280
  return { size: len, str: s };
242
281
  };
243
- Stream.prototype.parseOID = function (start, end, maxLength) {
282
+ Stream.prototype.parseOID = function (start, end, maxLength, isRelative) {
244
283
  var s = '',
245
284
  n = new Int10(),
246
285
  bits = 0;
@@ -251,7 +290,9 @@ Stream.prototype.parseOID = function (start, end, maxLength) {
251
290
  if (!(v & 0x80)) { // finished
252
291
  if (s === '') {
253
292
  n = n.simplify();
254
- if (n instanceof Int10) {
293
+ if (isRelative) {
294
+ s = (n instanceof Int10) ? n.toString() : "" + n;
295
+ } else if (n instanceof Int10) {
255
296
  n.sub(80);
256
297
  s = "2." + n.toString();
257
298
  } else {
@@ -268,7 +309,7 @@ Stream.prototype.parseOID = function (start, end, maxLength) {
268
309
  }
269
310
  if (bits > 0)
270
311
  s += ".incomplete";
271
- if (typeof oids === 'object') {
312
+ if (typeof oids === 'object' && !isRelative) {
272
313
  var oid = oids[s];
273
314
  if (oid) {
274
315
  if (oid.d) s += "\n" + oid.d;
@@ -278,6 +319,9 @@ Stream.prototype.parseOID = function (start, end, maxLength) {
278
319
  }
279
320
  return s;
280
321
  };
322
+ Stream.prototype.parseRelativeOID = function (start, end, maxLength) {
323
+ return this.parseOID(start, end, maxLength, true);
324
+ };
281
325
 
282
326
  function ASN1(stream, header, length, tag, tagLen, sub) {
283
327
  if (!(tag instanceof ASN1Tag)) throw 'Invalid tag value.';
@@ -305,6 +349,7 @@ ASN1.prototype.typeName = function () {
305
349
  case 0x0A: return "ENUMERATED";
306
350
  case 0x0B: return "EMBEDDED_PDV";
307
351
  case 0x0C: return "UTF8String";
352
+ case 0x0D: return "RELATIVE_OID";
308
353
  case 0x10: return "SEQUENCE";
309
354
  case 0x11: return "SET";
310
355
  case 0x12: return "NumericString";
@@ -379,6 +424,8 @@ ASN1.prototype.content = function (maxLength) {
379
424
  case 0x0A: // ENUMERATED
380
425
  return this.stream.parseInteger(content, content + len);
381
426
  //case 0x0B: // EMBEDDED_PDV
427
+ case 0x0D: // RELATIVE-OID
428
+ return this.stream.parseRelativeOID(content, content + len, maxLength);
382
429
  case 0x10: // SEQUENCE
383
430
  case 0x11: // SET
384
431
  if (this.sub !== null)
@@ -386,19 +433,20 @@ ASN1.prototype.content = function (maxLength) {
386
433
  else
387
434
  return "(no elem)";
388
435
  case 0x0C: // UTF8String
389
- return stringCut(this.stream.parseStringUTF(content, content + len), maxLength);
436
+ return recurse(this, 'parseStringUTF', maxLength).str;
437
+ case 0x14: // TeletexString
438
+ return recurse(this, 'parseStringT61', maxLength).str;
390
439
  case 0x12: // NumericString
391
440
  case 0x13: // PrintableString
392
- case 0x14: // TeletexString
393
441
  case 0x15: // VideotexString
394
442
  case 0x16: // IA5String
395
443
  case 0x1A: // VisibleString
396
444
  case 0x1B: // GeneralString
397
445
  //case 0x19: // GraphicString
398
446
  //case 0x1C: // UniversalString
399
- return stringCut(this.stream.parseStringISO(content, content + len), maxLength);
447
+ return recurse(this, 'parseStringISO', maxLength).str;
400
448
  case 0x1E: // BMPString
401
- return stringCut(this.stream.parseStringBMP(content, content + len), maxLength);
449
+ return recurse(this, 'parseStringBMP', maxLength).str;
402
450
  case 0x17: // UTCTime
403
451
  case 0x18: // GeneralizedTime
404
452
  return this.stream.parseTime(content, content + len, (this.tag.tagNumber == 0x17));
package/base64.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // Base64 JavaScript decoder
2
- // Copyright (c) 2008-2021 Lapo Luchini <lapo@lapo.it>
2
+ // Copyright (c) 2008-2022 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
package/hex.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // Hex JavaScript decoder
2
- // Copyright (c) 2008-2021 Lapo Luchini <lapo@lapo.it>
2
+ // Copyright (c) 2008-2022 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
package/int10.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // Big integer base-10 printing library
2
- // Copyright (c) 2008-2021 Lapo Luchini <lapo@lapo.it>
2
+ // Copyright (c) 2008-2022 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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lapo/asn1js",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "description": "Generic ASN.1 parser/decoder that can decode any valid ASN.1 DER or BER structures.",
5
5
  "main": "asn1.js",
6
6
  "repository": {
@@ -14,7 +14,7 @@
14
14
  "homepage": "https://lapo.it/asn1js/",
15
15
  "files": [ "asn1.js", "base64.js", "hex.js", "int10.js", "oids.js" ],
16
16
  "scripts" : {
17
- "lint" : "npx eslint asn1.js base64.js hex.js int10.js oids.js",
17
+ "lint" : "npx eslint asn1.js base64.js hex.js int10.js oids.js parseRFC.js dumpASN1.js",
18
18
  "test" : "node test"
19
19
  },
20
20
  "eslintConfig": {
@@ -36,7 +36,10 @@
36
36
  },
37
37
  "overrides": [
38
38
  {
39
- "files": [ "test.js" ],
39
+ "files": [ "test.js", "parseRFC.js", "dumpASN1.js" ],
40
+ "parserOptions": {
41
+ "ecmaVersion": 2020
42
+ },
40
43
  "rules": {
41
44
  "strict": [ "error", "global" ],
42
45
  "comma-dangle": [ "error", "always-multiline" ]