@lapo/asn1js 2.0.2 → 2.0.3
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/README.md +27 -7
- package/defs.js +138 -0
- package/package.json +17 -4
- package/rfcdef.js +10377 -0
package/README.md
CHANGED
|
@@ -5,8 +5,8 @@ asn1js is a JavaScript generic ASN.1 parser/decoder that can decode any valid AS
|
|
|
5
5
|
|
|
6
6
|
An example page that can decode Base64-encoded (raw base64, PEM armoring and `begin-base64` are recognized) or Hex-encoded (or local files with some browsers) is included and can be used both [online on the official website](https://lapo.it/asn1js/) or [offline (ZIP file)](https://lapo.it/asn1js/asn1js.zip).
|
|
7
7
|
|
|
8
|
-
Usage with `
|
|
9
|
-
|
|
8
|
+
Usage with `nodejs`
|
|
9
|
+
-------------------
|
|
10
10
|
|
|
11
11
|
This package can be installed with either npm or yarn via the following commands:
|
|
12
12
|
|
|
@@ -17,19 +17,38 @@ pnpm install @lapo/asn1js
|
|
|
17
17
|
yarn add @lapo/asn1js
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
You can import the classes like this:
|
|
21
21
|
|
|
22
22
|
```js
|
|
23
|
-
import ASN1 from '@lapo/asn1js';
|
|
23
|
+
import { ASN1 } from '@lapo/asn1js';
|
|
24
24
|
```
|
|
25
25
|
|
|
26
26
|
A submodule of this package can also be imported:
|
|
27
27
|
|
|
28
28
|
```js
|
|
29
|
-
import Hex from '@lapo/asn1js/hex';
|
|
29
|
+
import { Hex } from '@lapo/asn1js/hex.js';
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
If your code is still not using ES6 Modules (and is using CommonJS) you can `require` it normally [since NodeJS 22](https://joyeecheung.github.io/blog/2024/03/18/require-esm-in-node-js/) (with parameter `--experimental-require-module`):
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
const
|
|
36
|
+
{ ASN1 } = require('@lapo/asn1js'),
|
|
37
|
+
{ Hex } = require('@lapo/asn1js/hex.js');
|
|
38
|
+
console.log(ASN1.decode(Hex.decode('06032B6570')).content());
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
On older NodeJS you instead need to use async `import`:
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
async function main() {
|
|
45
|
+
const
|
|
46
|
+
{ ASN1 } = await import('@lapo/asn1js'),
|
|
47
|
+
{ Hex } = await import('@lapo/asn1js/hex.js');
|
|
48
|
+
console.log(ASN1.decode(Hex.decode('06032B6570')).content());
|
|
49
|
+
}
|
|
50
|
+
main();
|
|
51
|
+
```
|
|
33
52
|
|
|
34
53
|
Usage on the web
|
|
35
54
|
--------------------
|
|
@@ -38,7 +57,7 @@ Can be [tested on JSFiddle](https://jsfiddle.net/lapo/y6t2wo7q/).
|
|
|
38
57
|
|
|
39
58
|
```html
|
|
40
59
|
<script>
|
|
41
|
-
import { ASN1} from 'https://unpkg.com/@lapo/asn1js@2.0.0/asn1.js';
|
|
60
|
+
import { ASN1 } from 'https://unpkg.com/@lapo/asn1js@2.0.0/asn1.js';
|
|
42
61
|
import { Hex } from 'https://unpkg.com/@lapo/asn1js@2.0.0/hex.js';
|
|
43
62
|
|
|
44
63
|
document.body.innerText = ASN1.decode(Hex.decode('06032B6570')).content();
|
|
@@ -70,6 +89,7 @@ links
|
|
|
70
89
|
|
|
71
90
|
- [official website](https://lapo.it/asn1js/)
|
|
72
91
|
- [dedicated domain](https://asn1js.eu/)
|
|
92
|
+
- [single-file version working locally](https://asn1js.eu/index-local.html) (just save this link)
|
|
73
93
|
- [InDefero tracker](http://idf.lapo.it/p/asn1js/)
|
|
74
94
|
- [GitHub mirror](https://github.com/lapo-luchini/asn1js)
|
|
75
95
|
- [Ohloh code stats](https://www.openhub.net/p/asn1js)
|
package/defs.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// ASN.1 RFC definitions matcher
|
|
2
|
+
// Copyright (c) 2023-2024 Lapo Luchini <lapo@lapo.it>
|
|
3
|
+
|
|
4
|
+
// Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
+
// purpose with or without fee is hereby granted, provided that the above
|
|
6
|
+
// copyright notice and this permission notice appear in all copies.
|
|
7
|
+
//
|
|
8
|
+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
9
|
+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
10
|
+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
11
|
+
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
12
|
+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
13
|
+
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
14
|
+
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
15
|
+
|
|
16
|
+
import { rfcdef } from './rfcdef.js';
|
|
17
|
+
|
|
18
|
+
function translate(def, tn, stats) {
|
|
19
|
+
if (def?.type == 'tag' && !def.explicit)
|
|
20
|
+
// def.type = def.content[0].type;
|
|
21
|
+
def = def.content[0].type;
|
|
22
|
+
if (def?.definedBy)
|
|
23
|
+
try {
|
|
24
|
+
// hope current OIDs contain the type name (will need to parse from RFC itself)
|
|
25
|
+
def = Defs.searchType(firstUpper(stats.defs[def.definedBy][1]));
|
|
26
|
+
} catch (e) { /*ignore*/ }
|
|
27
|
+
while (def?.type == 'defined' || def?.type?.type == 'defined') {
|
|
28
|
+
const name = def?.type?.type ? def.type.name : def.name;
|
|
29
|
+
def = Object.assign({}, def);
|
|
30
|
+
def.type = Defs.searchType(name).type;
|
|
31
|
+
}
|
|
32
|
+
if (def?.type?.name == 'CHOICE') {
|
|
33
|
+
for (let c of def.type.content) {
|
|
34
|
+
if (tn != c.type.name && tn != c.name)
|
|
35
|
+
c = translate(c);
|
|
36
|
+
if (tn == c.type.name || tn == c.name) {
|
|
37
|
+
def = Object.assign({}, def);
|
|
38
|
+
def.type = c.type.name ? c.type : c;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const id = def?.id;
|
|
44
|
+
if (id)
|
|
45
|
+
def = Object.assign({}, def, { id });
|
|
46
|
+
return def ?? { type: {} };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function firstUpper(s) {
|
|
50
|
+
return s[0].toUpperCase() + s.slice(1);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export class Defs {
|
|
54
|
+
|
|
55
|
+
static moduleAndType(mod, name) {
|
|
56
|
+
return Object.assign({ module: { oid: mod.oid, name: mod.name, source: mod.source } }, mod.types[name]);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static searchType(name) {
|
|
60
|
+
for (const mod of Object.values(rfcdef))
|
|
61
|
+
if (name in mod.types) {
|
|
62
|
+
// console.log(name + ' found in ' + r.name);
|
|
63
|
+
// return r.types[name];
|
|
64
|
+
return Defs.moduleAndType(mod, name);
|
|
65
|
+
}
|
|
66
|
+
throw new Error('Type not found: ' + name);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static match(value, def, stats = { total: 0, recognized: 0, defs: {} }) {
|
|
70
|
+
value.def = {};
|
|
71
|
+
let tn = value.typeName().replaceAll('_', ' ');
|
|
72
|
+
def = translate(def, tn, stats);
|
|
73
|
+
++stats.total;
|
|
74
|
+
if (def?.type) {
|
|
75
|
+
// if (def.id || def.name) ++stats.recognized;
|
|
76
|
+
if (tn == def.type.name || tn == def.name || def.name == 'ANY')
|
|
77
|
+
++stats.recognized;
|
|
78
|
+
else if (def.name)
|
|
79
|
+
def = Object.assign({ mismatch: 1 }, def);
|
|
80
|
+
value.def = def;
|
|
81
|
+
}
|
|
82
|
+
if (value.sub !== null) {
|
|
83
|
+
if (def?.type?.type)
|
|
84
|
+
def = def.type;
|
|
85
|
+
let j = def?.content ? 0 : -1;
|
|
86
|
+
for (const subval of value.sub) {
|
|
87
|
+
let type;
|
|
88
|
+
if (j >= 0) {
|
|
89
|
+
if (def.typeOf)
|
|
90
|
+
type = def.content[0];
|
|
91
|
+
else {
|
|
92
|
+
let tn = subval.typeName().replaceAll('_', ' ');
|
|
93
|
+
for (;;) {
|
|
94
|
+
type = def.content[j++];
|
|
95
|
+
if (!type || typeof type != 'object') break;
|
|
96
|
+
if (type?.type?.type)
|
|
97
|
+
type = type.type;
|
|
98
|
+
if (type.type == 'defined') {
|
|
99
|
+
let t2 = translate(type, tn);
|
|
100
|
+
if (t2.type.name == tn) break; // exact match
|
|
101
|
+
if (t2.type.name == 'ANY') break; // good enough
|
|
102
|
+
}
|
|
103
|
+
if (type.name == tn) break; // exact match
|
|
104
|
+
if (type.name == 'ANY') break; // good enough
|
|
105
|
+
if (!('optional' in type || 'default' in type)) break;
|
|
106
|
+
}
|
|
107
|
+
if (type?.type == 'builtin' || type?.type == 'defined') {
|
|
108
|
+
let v = subval.content();
|
|
109
|
+
if (typeof v == 'string')
|
|
110
|
+
v = v.split(/\n/);
|
|
111
|
+
stats.defs[type.id] = v;
|
|
112
|
+
} else if (type?.definedBy && stats.defs?.[type.definedBy]?.[1]) { // hope current OIDs contain the type name (will need to parse from RFC itself)
|
|
113
|
+
try {
|
|
114
|
+
type = Defs.searchType(firstUpper(stats.defs[type.definedBy][1]));
|
|
115
|
+
} catch (e) { /*ignore*/ }
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
Defs.match(subval, type, stats);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return stats;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
Defs.RFC = rfcdef;
|
|
128
|
+
|
|
129
|
+
Defs.commonTypes = [
|
|
130
|
+
[ 'X.509 certificate', '1.3.6.1.5.5.7.0.18', 'Certificate' ],
|
|
131
|
+
[ 'X.509 public key info', '1.3.6.1.5.5.7.0.18', 'SubjectPublicKeyInfo' ],
|
|
132
|
+
[ 'CMS / PKCS#7 envelope', '1.2.840.113549.1.9.16.0.14', 'ContentInfo' ],
|
|
133
|
+
[ 'PKCS#1 RSA private key', '1.2.840.113549.1.1.0.1', 'RSAPrivateKey' ],
|
|
134
|
+
[ 'PKCS#8 encrypted private key', '1.2.840.113549.1.8.1.1', 'EncryptedPrivateKeyInfo' ],
|
|
135
|
+
[ 'PKCS#8 private key', '1.2.840.113549.1.8.1.1', 'PrivateKeyInfo' ],
|
|
136
|
+
[ 'PKCS#10 certification request', '1.2.840.113549.1.10.1.1', 'CertificationRequest' ],
|
|
137
|
+
[ 'CMP PKI Message', '1.3.6.1.5.5.7.0.16', 'PKIMessage' ],
|
|
138
|
+
].map(arr => ({ description: arr[0], ...Defs.moduleAndType(rfcdef[arr[1]], arr[2]) }));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lapo/asn1js",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
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,10 +13,11 @@
|
|
|
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", "oids.js" ],
|
|
16
|
+
"files": [ "asn1.js", "base64.js", "hex.js", "int10.js", "defs.js", "oids.js", "rfcdef.js" ],
|
|
17
17
|
"scripts": {
|
|
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",
|
|
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",
|
|
20
|
+
"build": "vite build",
|
|
20
21
|
"serve": "echo 'Connect to http://localhost:3000/' ; npx statik --port 3000 .",
|
|
21
22
|
"test": "node test",
|
|
22
23
|
"testdefs": "node testDefs"
|
|
@@ -25,7 +26,19 @@
|
|
|
25
26
|
"node": ">=12.20.0"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
|
-
"eslint": "^8.34.0"
|
|
29
|
+
"eslint": "^8.34.0",
|
|
30
|
+
"htmlparser2": "^9.1.0",
|
|
31
|
+
"vite": "^5.2.10",
|
|
32
|
+
"vite-plugin-dom": "^1.0.3",
|
|
33
|
+
"vite-plugin-singlefile": "^2.0.1"
|
|
34
|
+
},
|
|
35
|
+
"overrides": {
|
|
36
|
+
"rollup": "npm:@rollup/wasm-node"
|
|
37
|
+
},
|
|
38
|
+
"pnpm": {
|
|
39
|
+
"overrides": {
|
|
40
|
+
"rollup": "npm:@rollup/wasm-node"
|
|
41
|
+
}
|
|
29
42
|
},
|
|
30
43
|
"eslintConfig": {
|
|
31
44
|
"env": {
|