forked from lapo-luchini/asn1js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumpASN1.js
executable file
·67 lines (64 loc) · 2.23 KB
/
dumpASN1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env node
'use strict';
const
fs = require('fs'),
Base64 = require('./base64'),
ASN1 = require('./asn1'),
Defs = require('./defs'),
colYellow = '\x1b[33m',
colBlue = '\x1b[34m',
colReset = '\x1b[0m';
function print(value, indent) {
if (indent === undefined) indent = '';
const def = value.def;
let name = '';
if (def?.type) {
if (def.id) name += colBlue + def.id + colReset;
if (typeof def.type == 'object' && def.name) name = (name ? name + ' ' : '') + def.name;
if (name) name += ' ';
}
let s = indent + name + colYellow + value.typeName() + colReset + ' @' + value.stream.pos;
if (value.length >= 0)
s += '+';
s += value.length;
if (value.tag.tagConstructed)
s += ' (constructed)';
else if ((value.tag.isUniversal() && ((value.tag.tagNumber == 0x03) || (value.tag.tagNumber == 0x04))) && (value.sub !== null))
s += ' (encapsulates)';
let content = value.content();
if (content)
s += ': ' + content.replace(/\n/g, '|');
s += '\n';
if (value.sub !== null) {
indent += ' ';
for (const subval of value.sub)
s += print(subval, indent);
}
return s;
}
let content = fs.readFileSync(process.argv[2]);
try { // try PEM first
content = Base64.unarmor(content);
} catch (e) { // try DER/BER then
}
let result = ASN1.decode(content);
content = null;
const t0 = performance.now();
const types = Defs.commonTypes
.map(type => {
const stats = Defs.match(result, type);
return { type, match: stats.recognized / stats.total };
})
.sort((a, b) => b.match - a.match);
const t1 = performance.now();
console.log('Parsed in ' + (t1 - t0).toFixed(2) + ' ms; possible types:');
for (const t of types)
console.log((t.match * 100).toFixed(2).padStart(6) + '% ' + t.type.description);
Defs.match(result, types[0].match >= 0.1 ? types[0].type : null);
console.log('Parsed as:', result.def);
// const type = searchType(process.argv[2]);
// const stats = applyDef(result, type);
console.log(print(result));
// console.log('Stats:', (stats.recognized * 100 / stats.total).toFixed(2) + '%');
// // print(result, searchType(process.argv[2]), stats);
// // console.log('Defs:', stats.defs);