@lapo/asn1js 2.0.3 → 2.0.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.
Files changed (3) hide show
  1. package/README.md +14 -1
  2. package/dumpASN1.js +79 -0
  3. package/package.json +5 -2
package/README.md CHANGED
@@ -64,6 +64,20 @@ document.body.innerText = ASN1.decode(Hex.decode('06032B6570')).content();
64
64
  </script>
65
65
  ```
66
66
 
67
+ Local usage
68
+ --------------------
69
+
70
+ Since unfortunately ESM modules are not working on `file:` protocol due to [CORS issues](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#other_differences_between_modules_and_standard_scripts), there is a bundled [single-file version working locally](https://asn1js.eu/index-local.html). It doesn't work online (due to [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) restrictions about inline content) but can be saved locally and opened in a browser. ([known bug](https://github.com/lapo-luchini/asn1js/issues/89): dark mode is currently broken in this mode)
71
+
72
+ Usage from CLI
73
+ --------------------
74
+
75
+ You can dump an ASN.1 structure from the command line using the following command (no need to even install it):
76
+
77
+ ```sh
78
+ npx @lapo/asn1js ed25519.cer
79
+ ```
80
+
67
81
  ISC license
68
82
  -----------
69
83
 
@@ -89,7 +103,6 @@ links
89
103
 
90
104
  - [official website](https://lapo.it/asn1js/)
91
105
  - [dedicated domain](https://asn1js.eu/)
92
- - [single-file version working locally](https://asn1js.eu/index-local.html) (just save this link)
93
106
  - [InDefero tracker](http://idf.lapo.it/p/asn1js/)
94
107
  - [GitHub mirror](https://github.com/lapo-luchini/asn1js)
95
108
  - [Ohloh code stats](https://www.openhub.net/p/asn1js)
package/dumpASN1.js ADDED
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env node
2
+
3
+ import * as fs from 'node:fs';
4
+ import { Base64 } from './base64.js';
5
+ import { ASN1 } from './asn1.js';
6
+ import { Defs } from './defs.js';
7
+
8
+ const
9
+ colYellow = '\x1b[33m',
10
+ colBlue = '\x1b[34m',
11
+ colReset = '\x1b[0m',
12
+ reDataURI = /^data:(?:[a-z-]+[/][a-z.+-]+;)?base64,([A-Za-z0-9+/=\s]+)$/;
13
+
14
+ function print(value, indent) {
15
+ if (indent === undefined) indent = '';
16
+ const def = value.def;
17
+ let name = '';
18
+ if (def?.type) {
19
+ if (def.id) name += colBlue + def.id + colReset;
20
+ if (typeof def.type == 'object' && def.name) name = (name ? name + ' ' : '') + def.name;
21
+ if (def.mismatch) name = (name ? name + ' ' : '') + '[?]';
22
+ if (name) name += ' ';
23
+ }
24
+ let s = indent + name + colYellow + value.typeName() + colReset + ' @' + value.stream.pos;
25
+ if (value.length >= 0)
26
+ s += '+';
27
+ s += value.length;
28
+ if (value.tag.tagConstructed)
29
+ s += ' (constructed)';
30
+ else if ((value.tag.isUniversal() && ((value.tag.tagNumber == 0x03) || (value.tag.tagNumber == 0x04))) && (value.sub !== null))
31
+ s += ' (encapsulates)';
32
+ let content = value.content();
33
+ if (content)
34
+ s += ': ' + content.replace(/\n/g, '|');
35
+ s += '\n';
36
+ if (value.sub !== null) {
37
+ indent += ' ';
38
+ for (const subval of value.sub)
39
+ s += print(subval, indent);
40
+ }
41
+ return s;
42
+ }
43
+
44
+ const filename = process.argv[2];
45
+ const match = reDataURI.exec(filename);
46
+ let content = match
47
+ ? Buffer.from(match[1])
48
+ : fs.readFileSync(filename);
49
+ try { // try PEM first
50
+ content = Base64.unarmor(content);
51
+ } catch (e) { // try DER/BER then
52
+ }
53
+ let result = ASN1.decode(content);
54
+ content = null;
55
+ const t0 = performance.now();
56
+ if (process.argv.length == 5) {
57
+ Defs.match(result, Defs.moduleAndType(Defs.RFC[process.argv[3]], process.argv[4]));
58
+ } else {
59
+ const types = Defs.commonTypes
60
+ .map(type => {
61
+ const stats = Defs.match(result, type);
62
+ return { type, match: stats.recognized / stats.total };
63
+ })
64
+ .sort((a, b) => b.match - a.match);
65
+ const t1 = performance.now();
66
+ console.log('Parsed in ' + (t1 - t0).toFixed(2) + ' ms; possible types:');
67
+ for (const t of types)
68
+ console.log((t.match * 100).toFixed(2).padStart(6) + '% ' + t.type.description);
69
+ Defs.match(result, types[0].type);
70
+ // const stats = Defs.match(result, types[0].type);
71
+ // console.log('Stats:', stats);
72
+ console.log('Parsed as:', result.def);
73
+ // const type = searchType(process.argv[2]);
74
+ // const stats = applyDef(result, type);
75
+ }
76
+ console.log(print(result));
77
+ // console.log('Stats:', (stats.recognized * 100 / stats.total).toFixed(2) + '%');
78
+ // // print(result, searchType(process.argv[2]), stats);
79
+ // // console.log('Defs:', stats.defs);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lapo/asn1js",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
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",
@@ -13,7 +13,7 @@
13
13
  "license": "ISC",
14
14
  "bugs": { "url": "https://github.com/lapo-luchini/asn1js/issues" },
15
15
  "homepage": "https://lapo.it/asn1js/",
16
- "files": [ "asn1.js", "base64.js", "hex.js", "int10.js", "defs.js", "oids.js", "rfcdef.js" ],
16
+ "files": [ "asn1.js", "base64.js", "hex.js", "int10.js", "defs.js", "oids.js", "rfcdef.js", "dumpASN1.js" ],
17
17
  "scripts": {
18
18
  "lint": "npx eslint asn1.js base64.js hex.js int10.js defs.js oids.js rfcdef.js tags.js context.js index.js parseRFC.js dumpASN1.js test.js testDefs.js vite.config.js",
19
19
  "lint-action": "npx @action-validator/cli .github/workflows/node.js.yml",
@@ -22,6 +22,9 @@
22
22
  "test": "node test",
23
23
  "testdefs": "node testDefs"
24
24
  },
25
+ "bin": {
26
+ "dumpASN1": "./dumpASN1.js"
27
+ },
25
28
  "engines": {
26
29
  "node": ">=12.20.0"
27
30
  },