@lapo/asn1js 2.0.0 → 2.0.2

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 (4) hide show
  1. package/README.md +11 -15
  2. package/asn1.js +42 -20
  3. package/oids.js +0 -1
  4. package/package.json +19 -8
package/README.md CHANGED
@@ -12,40 +12,36 @@ This package can be installed with either npm or yarn via the following commands
12
12
 
13
13
  ```sh
14
14
  npm install @lapo/asn1js
15
- # or with yarn
15
+ # or other tools
16
+ pnpm install @lapo/asn1js
16
17
  yarn add @lapo/asn1js
17
18
  ```
18
19
 
19
20
  Assuming a standard javascript bundler is setup you can import it like so:
20
21
 
21
22
  ```js
22
- const ASN1 = require('@lapo/asn1js');
23
- // or with ES modules
24
23
  import ASN1 from '@lapo/asn1js';
25
24
  ```
26
25
 
27
26
  A submodule of this package can also be imported:
28
27
 
29
28
  ```js
30
- const Hex = require('@lapo/asn1js/hex');
31
- // or with ES modules
32
29
  import Hex from '@lapo/asn1js/hex';
33
30
  ```
34
31
 
35
- Usage with RequireJS
32
+ Unfortunately until [`require(esm)` gets released](https://joyeecheung.github.io/blog/2024/03/18/require-esm-in-node-js/) it is necessary to use async `import()` when used from CommonJS (legacy NodeJS) code.
33
+
34
+ Usage on the web
36
35
  --------------------
37
36
 
38
- Can be [tested on JSFiddle](https://jsfiddle.net/lapo/tmdq35ug/).
37
+ Can be [tested on JSFiddle](https://jsfiddle.net/lapo/y6t2wo7q/).
39
38
 
40
39
  ```html
41
- <script type="text/javascript" src="https://unpkg.com/requirejs/require.js"></script>
42
40
  <script>
43
- require([
44
- 'https://unpkg.com/@lapo/asn1js/asn1.js',
45
- 'https://unpkg.com/@lapo/asn1js/hex.js'
46
- ], function(ASN1, Hex) {
47
- document.body.innerText = ASN1.decode(Hex.decode('06032B6570')).content();
48
- });
41
+ import { ASN1} from 'https://unpkg.com/@lapo/asn1js@2.0.0/asn1.js';
42
+ import { Hex } from 'https://unpkg.com/@lapo/asn1js@2.0.0/hex.js';
43
+
44
+ document.body.innerText = ASN1.decode(Hex.decode('06032B6570')).content();
49
45
  </script>
50
46
  ```
51
47
 
@@ -66,7 +62,7 @@ credits
66
62
  - extended tag support added by [Péter Budai](https://www.peterbudai.eu/)
67
63
  - patches by [Gergely Nagy](https://github.com/ngg)
68
64
  - Relative OID support added by [Mistial Developer](https://github.com/mistial-dev)
69
- - dark mode support added by [Oliver Burgmaier](https://github.com/olibu/)
65
+ - dark mode and other UI improvements by [Oliver Burgmaier](https://github.com/olibu/)
70
66
  - patches by [Nicolai Søborg](https://github.com/NicolaiSoeborg)
71
67
 
72
68
  links
package/asn1.js CHANGED
@@ -38,7 +38,7 @@ const
38
38
  ['', ''],
39
39
  ['OUou', 'ŐŰőű'], // Double Acute
40
40
  ['AEIUaeiu', 'ĄĘĮŲąęįų'], // Ogonek
41
- ['CDELNRSTZcdelnrstz', 'ČĎĚĽŇŘŠŤŽčďěľňřšťž'] // Caron
41
+ ['CDELNRSTZcdelnrstz', 'ČĎĚĽŇŘŠŤŽčďěľňřšťž'], // Caron
42
42
  ];
43
43
 
44
44
  function stringCut(str, len) {
@@ -72,17 +72,21 @@ class Stream {
72
72
  if (pos === undefined)
73
73
  pos = this.pos++;
74
74
  if (pos >= this.enc.length)
75
- throw 'Requesting byte offset ' + pos + ' on a stream of length ' + this.enc.length;
75
+ throw new Error('Requesting byte offset ' + pos + ' on a stream of length ' + this.enc.length);
76
76
  return (typeof this.enc == 'string') ? this.enc.charCodeAt(pos) : this.enc[pos];
77
77
  }
78
78
  hexByte(b) {
79
79
  return hexDigits.charAt((b >> 4) & 0xF) + hexDigits.charAt(b & 0xF);
80
80
  }
81
- hexDump(start, end, raw) {
81
+ /** Hexadecimal dump.
82
+ * @param type 'raw', 'byte' or 'dump' */
83
+ hexDump(start, end, type = 'dump') {
82
84
  let s = '';
83
85
  for (let i = start; i < end; ++i) {
86
+ if (type == 'byte' && i > start)
87
+ s += ' ';
84
88
  s += this.hexByte(this.get(i));
85
- if (raw !== true)
89
+ if (type == 'dump')
86
90
  switch (i & 0xF) {
87
91
  case 0x7: s += ' '; break;
88
92
  case 0xF: s += '\n'; break;
@@ -190,7 +194,7 @@ class Stream {
190
194
  let s = this.parseStringISO(start, end).str,
191
195
  m = (shortYear ? reTimeS : reTimeL).exec(s);
192
196
  if (!m)
193
- return 'Unrecognized time: ' + s;
197
+ throw new Error('Unrecognized time: ' + s);
194
198
  if (shortYear) {
195
199
  // to avoid querying the timer, use the fixed range [1970, 2069]
196
200
  // it will conform with ITU X.400 [-10, +40] sliding window until 2030
@@ -245,7 +249,7 @@ class Stream {
245
249
  parseBitString(start, end, maxLength) {
246
250
  let unusedBits = this.get(start);
247
251
  if (unusedBits > 7)
248
- throw 'Invalid BitString with unusedBits=' + unusedBits;
252
+ throw new Error('Invalid BitString with unusedBits=' + unusedBits);
249
253
  let lenBit = ((end - start - 1) << 3) - unusedBits,
250
254
  s = '';
251
255
  for (let i = start + 1; i < end; ++i) {
@@ -368,7 +372,7 @@ class ASN1Tag {
368
372
 
369
373
  export class ASN1 {
370
374
  constructor(stream, header, length, tag, tagLen, sub) {
371
- if (!(tag instanceof ASN1Tag)) throw 'Invalid tag value.';
375
+ if (!(tag instanceof ASN1Tag)) throw new Error('Invalid tag value.');
372
376
  this.stream = stream;
373
377
  this.header = header;
374
378
  this.length = length;
@@ -485,7 +489,16 @@ export class ASN1 {
485
489
  }
486
490
  toPrettyString(indent) {
487
491
  if (indent === undefined) indent = '';
488
- let s = indent + this.typeName() + ' @' + this.stream.pos;
492
+ let s = indent;
493
+ if (this.def) {
494
+ if (this.def.id)
495
+ s += this.def.id + ' ';
496
+ if (this.def.name && this.def.name != this.typeName().replace(/_/g, ' '))
497
+ s+= this.def.name + ' ';
498
+ if (this.def.mismatch)
499
+ s += '[?] ';
500
+ }
501
+ s += this.typeName() + ' @' + this.stream.pos;
489
502
  if (this.length >= 0)
490
503
  s += '+';
491
504
  s += this.length;
@@ -517,9 +530,12 @@ export class ASN1 {
517
530
  posLen() {
518
531
  return this.stream.pos + this.tagLen;
519
532
  }
520
- toHexString() {
521
- return this.stream.hexDump(this.posStart(), this.posEnd(), true);
533
+ /** Hexadecimal dump of the node.
534
+ * @param type 'raw', 'byte' or 'dump' */
535
+ toHexString(type = 'raw') {
536
+ return this.stream.hexDump(this.posStart(), this.posEnd(), type);
522
537
  }
538
+ /** Base64 dump of the node. */
523
539
  toB64String() {
524
540
  return this.stream.b64Dump(this.posStart(), this.posEnd());
525
541
  }
@@ -531,7 +547,7 @@ export class ASN1 {
531
547
  if (len === 0) // long form with length 0 is a special case
532
548
  return null; // undefined length
533
549
  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);
550
+ throw new Error('Length over 48 bits not supported at position ' + (stream.pos - 1));
535
551
  buf = 0;
536
552
  for (let i = 0; i < len; ++i)
537
553
  buf = (buf * 256) + stream.get();
@@ -539,7 +555,7 @@ export class ASN1 {
539
555
  }
540
556
  static decode(stream, offset, type = ASN1) {
541
557
  if (!(type == ASN1 || type.prototype instanceof ASN1))
542
- throw 'Must pass a class that extends ASN1';
558
+ throw new Error('Must pass a class that extends ASN1');
543
559
  if (!(stream instanceof Stream))
544
560
  stream = new Stream(stream, offset || 0);
545
561
  let streamStart = new Stream(stream),
@@ -555,11 +571,11 @@ export class ASN1 {
555
571
  // definite length
556
572
  let end = start + len;
557
573
  if (end > stream.enc.length)
558
- throw 'Container at offset ' + start + ' has a length of ' + len + ', which is past the end of the stream';
574
+ throw new Error('Container at offset ' + start + ' has a length of ' + len + ', which is past the end of the stream');
559
575
  while (stream.pos < end)
560
576
  sub[sub.length] = type.decode(stream);
561
577
  if (stream.pos != end)
562
- throw 'Content size is not correct for container at offset ' + start;
578
+ throw new Error('Content size is not correct for container at offset ' + start);
563
579
  } else {
564
580
  // undefined length
565
581
  try {
@@ -571,7 +587,7 @@ export class ASN1 {
571
587
  }
572
588
  len = start - stream.pos; // undefined lengths are represented as negative values
573
589
  } catch (e) {
574
- throw 'Exception while decoding undefined length content at offset ' + start + ': ' + e;
590
+ throw new Error('Exception while decoding undefined length content at offset ' + start + ': ' + e);
575
591
  }
576
592
  }
577
593
  };
@@ -583,11 +599,17 @@ export class ASN1 {
583
599
  try {
584
600
  if (tag.tagNumber == 0x03)
585
601
  if (stream.get() != 0)
586
- throw 'BIT STRINGs with unused bits cannot encapsulate.';
602
+ throw new Error('BIT STRINGs with unused bits cannot encapsulate.');
587
603
  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.';
604
+ for (let s of sub) {
605
+ if (s.tag.isEOC())
606
+ throw new Error('EOC is not supposed to be actual content.');
607
+ try {
608
+ s.content();
609
+ } catch (e) {
610
+ throw new Error('Unable to parse content: ' + e);
611
+ }
612
+ }
591
613
  } catch (e) {
592
614
  // but silently ignore when they don't
593
615
  sub = null;
@@ -596,7 +618,7 @@ export class ASN1 {
596
618
  }
597
619
  if (sub === null) {
598
620
  if (len === null)
599
- throw "We can't skip over an invalid tag with undefined length at offset " + start;
621
+ throw new Error("We can't skip over an invalid tag with undefined length at offset " + start);
600
622
  stream.pos = start + Math.abs(len);
601
623
  }
602
624
  return new type(streamStart, header, len, tag, tagLen, sub);
package/oids.js CHANGED
@@ -2722,5 +2722,4 @@ export const oids = {
2722
2722
  "1.3.6.1.4.1.40869.1.1.22.3": { "d": "TWCA EV policy", "c": "TWCA Root Certification Authority" },
2723
2723
  "2.16.840.1.113733.1.7.23.6": { "d": "VeriSign EV policy", "c": "VeriSign Class 3 Public Primary Certification Authority" },
2724
2724
  "2.16.840.1.114171.500.9": { "d": "Wells Fargo EV policy", "c": "Wells Fargo WellsSecure Public Root Certificate Authority" },
2725
- "END": ""
2726
2725
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lapo/asn1js",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Generic ASN.1 parser/decoder that can decode any valid ASN.1 DER or BER structures.",
5
5
  "type": "module",
6
6
  "main": "asn1.js",
@@ -15,10 +15,11 @@
15
15
  "homepage": "https://lapo.it/asn1js/",
16
16
  "files": [ "asn1.js", "base64.js", "hex.js", "int10.js", "oids.js" ],
17
17
  "scripts": {
18
- "lint": "npx eslint asn1.js base64.js hex.js int10.js oids.js tags.js index.js parseRFC.js dumpASN1.js",
18
+ "lint": "npx eslint asn1.js base64.js hex.js int10.js oids.js tags.js context.js index.js parseRFC.js dumpASN1.js test.js testDefs.js",
19
19
  "lint-action": "npx @action-validator/cli .github/workflows/node.js.yml",
20
20
  "serve": "echo 'Connect to http://localhost:3000/' ; npx statik --port 3000 .",
21
- "test": "node test"
21
+ "test": "node test",
22
+ "testdefs": "node testDefs"
22
23
  },
23
24
  "engines": {
24
25
  "node": ">=12.20.0"
@@ -47,17 +48,21 @@
47
48
  "semi": [ "warn", "always" ],
48
49
  "quotes": [ "error", "single", { "avoidEscape": true } ],
49
50
  "no-var": [ "warn" ],
50
- "comma-dangle": [ "error", "never" ]
51
+ "comma-dangle": [ "error", "always-multiline" ]
51
52
  },
52
53
  "overrides": [
53
54
  {
55
+ "files": [ "defs.js" ],
56
+ "parserOptions": {
57
+ "ecmaVersion": 2020
58
+ }
59
+ }, {
54
60
  "files": [ "test.js", "parseRFC.js", "dumpASN1.js" ],
55
61
  "parserOptions": {
56
62
  "ecmaVersion": 2021
57
63
  },
58
64
  "rules": {
59
- "strict": [ "error", "global" ],
60
- "comma-dangle": [ "error", "always-multiline" ]
65
+ "strict": [ "error", "global" ]
61
66
  }
62
67
  }, {
63
68
  "files": [ "oids.js" ],
@@ -66,9 +71,10 @@
66
71
  "quotes": [ "warn", "double" ]
67
72
  }
68
73
  }, {
69
- "files": [ "tags.js" ],
74
+ "files": [ "tags.js", "rfcdef.js" ],
70
75
  "rules": {
71
- "comma-dangle": [ "error", "always-multiline" ],
76
+ "indent": [ "error", 2, { "ignoredNodes": [ "Program > ExpressionStatement > CallExpression > FunctionExpression > BlockStatement > ExpressionStatement[directive='use strict']:first-child" ] } ],
77
+ "comma-dangle": "off",
72
78
  "quotes": [ "warn", "double" ]
73
79
  }
74
80
  }, {
@@ -76,6 +82,11 @@
76
82
  "parserOptions": {
77
83
  "ecmaVersion": 2021
78
84
  }
85
+ }, {
86
+ "files": [ "testDefs.js" ],
87
+ "parserOptions": {
88
+ "ecmaVersion": 2022
89
+ }
79
90
  }
80
91
  ]
81
92
  }