/
defs.js
138 lines (127 loc) · 5.8 KB
/
defs.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// ASN.1 RFC definitions matcher
// Copyright (c) 2023-2024 Lapo Luchini <lapo@lapo.it>
// 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.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import { rfcdef } from './rfcdef.js';
function translate(def, tn, stats) {
if (def?.type == 'tag' && !def.explicit)
// def.type = def.content[0].type;
def = def.content[0].type;
if (def?.definedBy)
try {
// hope current OIDs contain the type name (will need to parse from RFC itself)
def = Defs.searchType(firstUpper(stats.defs[def.definedBy][1]));
} catch (e) { /*ignore*/ }
while (def?.type == 'defined' || def?.type?.type == 'defined') {
const name = def?.type?.type ? def.type.name : def.name;
def = Object.assign({}, def);
def.type = Defs.searchType(name).type;
}
if (def?.type?.name == 'CHOICE') {
for (let c of def.type.content) {
if (tn != c.type.name && tn != c.name)
c = translate(c);
if (tn == c.type.name || tn == c.name) {
def = Object.assign({}, def);
def.type = c.type.name ? c.type : c;
break;
}
}
}
const id = def?.id;
if (id)
def = Object.assign({}, def, { id });
return def ?? { type: {} };
}
function firstUpper(s) {
return s[0].toUpperCase() + s.slice(1);
}
export class Defs {
static moduleAndType(mod, name) {
return Object.assign({ module: { oid: mod.oid, name: mod.name, source: mod.source } }, mod.types[name]);
}
static searchType(name) {
for (const mod of Object.values(rfcdef))
if (name in mod.types) {
// console.log(name + ' found in ' + r.name);
// return r.types[name];
return Defs.moduleAndType(mod, name);
}
throw new Error('Type not found: ' + name);
}
static match(value, def, stats = { total: 0, recognized: 0, defs: {} }) {
value.def = {};
let tn = value.typeName().replaceAll('_', ' ');
def = translate(def, tn, stats);
++stats.total;
if (def?.type) {
// if (def.id || def.name) ++stats.recognized;
if (tn == def.type.name || tn == def.name || def.name == 'ANY')
++stats.recognized;
else if (def.name)
def = Object.assign({ mismatch: 1 }, def);
value.def = def;
}
if (value.sub !== null) {
if (def?.type?.type)
def = def.type;
let j = def?.content ? 0 : -1;
for (const subval of value.sub) {
let type;
if (j >= 0) {
if (def.typeOf)
type = def.content[0];
else {
let tn = subval.typeName().replaceAll('_', ' ');
for (;;) {
type = def.content[j++];
if (!type || typeof type != 'object') break;
if (type?.type?.type)
type = type.type;
if (type.type == 'defined') {
let t2 = translate(type, tn);
if (t2.type.name == tn) break; // exact match
if (t2.type.name == 'ANY') break; // good enough
}
if (type.name == tn) break; // exact match
if (type.name == 'ANY') break; // good enough
if (!('optional' in type || 'default' in type)) break;
}
if (type?.type == 'builtin' || type?.type == 'defined') {
let v = subval.content();
if (typeof v == 'string')
v = v.split(/\n/);
stats.defs[type.id] = v;
} else if (type?.definedBy && stats.defs?.[type.definedBy]?.[1]) { // hope current OIDs contain the type name (will need to parse from RFC itself)
try {
type = Defs.searchType(firstUpper(stats.defs[type.definedBy][1]));
} catch (e) { /*ignore*/ }
}
}
}
Defs.match(subval, type, stats);
}
}
return stats;
}
}
Defs.RFC = rfcdef;
Defs.commonTypes = [
[ 'X.509 certificate', '1.3.6.1.5.5.7.0.18', 'Certificate' ],
[ 'X.509 public key info', '1.3.6.1.5.5.7.0.18', 'SubjectPublicKeyInfo' ],
[ 'CMS / PKCS#7 envelope', '1.2.840.113549.1.9.16.0.14', 'ContentInfo' ],
[ 'PKCS#1 RSA private key', '1.2.840.113549.1.1.0.1', 'RSAPrivateKey' ],
[ 'PKCS#8 encrypted private key', '1.2.840.113549.1.8.1.1', 'EncryptedPrivateKeyInfo' ],
[ 'PKCS#8 private key', '1.2.840.113549.1.8.1.1', 'PrivateKeyInfo' ],
[ 'PKCS#10 certification request', '1.2.840.113549.1.10.1.1', 'CertificationRequest' ],
[ 'CMP PKI Message', '1.3.6.1.5.5.7.0.16', 'PKIMessage' ],
].map(arr => ({ description: arr[0], ...Defs.moduleAndType(rfcdef[arr[1]], arr[2]) }));