1// ASN.1 RFC definitions matcher
2// Copyright (c) 2023 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
16import { rfcdef } from './rfcdef.js';
17
18function 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?.name == 'CHOICE' || def?.type?.name == 'CHOICE') {
33 for (let c of def.content ?? 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 if (c.id) def.id = c.id;
39 def.type = c.type.name ? c.type : c;
40 break;
41 }
42 }
43 }
44 const id = def?.id;
45 if (id)
46 def = Object.assign({}, def, { id });
47 return def ?? { type: {} };
48}
49
50function firstUpper(s) {
51 return s[0].toUpperCase() + s.slice(1);
52}
53
54export class Defs {
55
56 static moduleAndType(mod, name) {
57 return Object.assign({ module: { oid: mod.oid, name: mod.name, source: mod.source } }, mod.types[name]);
58 }
59
60 static searchType(name) {
61 for (const mod of Object.values(rfcdef))
62 if (name in mod.types) {
63 // console.log(name + ' found in ' + r.name);
64 // return r.types[name];
65 return Defs.moduleAndType(mod, name);
66 }
67 throw new Error('Type not found: ' + name);
68 }
69
70 static match(value, def, stats = { total: 0, recognized: 0, defs: {} }) {
71 value.def = {};
72 let tn = value.typeName().replaceAll('_', ' ');
73 def = translate(def, tn, stats);
74 ++stats.total;
75 if (def?.type) {
76 // if (def.id || def.name) ++stats.recognized;
77 if (tn == def.type.name || tn == def.name || def.name == 'ANY')
78 ++stats.recognized;
79 else if (def.name)
80 def = Object.assign({ mismatch: 1 }, def);
81 value.def = def;
82 }
83 if (value.sub !== null) {
84 if (def?.type?.type)
85 def = def.type;
86 let j = def?.content ? 0 : -1;
87 for (const subval of value.sub) {
88 let type;
89 if (j >= 0) {
90 if (def.typeOf)
91 type = def.content[0];
92 else {
93 let tn = subval.typeName().replaceAll('_', ' ');
94 for (;;) {
95 type = def.content[j++];
96 if (!type || typeof type != 'object') break;
97 if (type?.type?.type)
98 type = type.type;
99 if (type.type == 'defined') {
100 let t2 = translate(type, tn);
101 if (t2.type.name == tn) break; // exact match
102 if (t2.type.name == 'ANY') break; // good enough
103 }
104 if (type.name == tn) break; // exact match
105 if (type.name == 'ANY') break; // good enough
106 if (!('optional' in type || 'default' in type)) break;
107 }
108 if (type?.type == 'builtin' || type?.type == 'defined') {
109 let v = subval.content();
110 if (typeof v == 'string')
111 v = v.split(/\n/);
112 stats.defs[type.id] = v;
113 } else if (type?.definedBy && stats.defs?.[type.definedBy]?.[1]) { // hope current OIDs contain the type name (will need to parse from RFC itself)
114 try {
115 type = Defs.searchType(firstUpper(stats.defs[type.definedBy][1]));
116 } catch (e) { /*ignore*/ }
117 }
118 }
119 }
120 Defs.match(subval, type, stats);
121 }
122 }
123 return stats;
124 }
125
126}
127
128Defs.RFC = rfcdef;
129
130Defs.commonTypes = [
131 [ 'X.509 certificate', '1.3.6.1.5.5.7.0.18', 'Certificate' ],
132 [ 'X.509 public key info', '1.3.6.1.5.5.7.0.18', 'SubjectPublicKeyInfo' ],
133 [ 'X.509 certificate revocation list', '1.3.6.1.5.5.7.0.18', 'CertificateList' ],
134 [ 'CMS / PKCS#7 envelope', '1.2.840.113549.1.9.16.0.14', 'ContentInfo' ],
135 [ 'PKCS#1 RSA private key', '1.2.840.113549.1.1.0.1', 'RSAPrivateKey' ],
136 [ 'PKCS#8 encrypted private key', '1.2.840.113549.1.8.1.1', 'EncryptedPrivateKeyInfo' ],
137 [ 'PKCS#8 private key', '1.2.840.113549.1.8.1.1', 'PrivateKeyInfo' ],
138 [ 'PKCS#10 certification request', '1.2.840.113549.1.10.1.1', 'CertificationRequest' ],
139 [ 'CMP PKI Message', '1.3.6.1.5.5.7.0.16', 'PKIMessage' ],
140 [ 'LDAP Message', '1.3.6.1.1.18', 'LDAPMessage' ],
141].map(arr => ({ description: arr[0], ...Defs.moduleAndType(rfcdef[arr[1]], arr[2]) }));