JavaScript generic ASN.1 parser (mirror)
at github-82 129 lines 5.4 kB view raw
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 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?.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 49function firstUpper(s) { 50 return s[0].toUpperCase() + s.slice(1); 51} 52 53export 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 '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 do { 94 type = def.content[j++]; 95 // type = translate(type, tn); 96 if (type?.type?.type) 97 type = type.type; 98 } while (type && typeof type == 'object' && ('optional' in type || 'default' in type) && type.name != 'ANY' && type.name != tn); 99 if (type?.type == 'builtin' || type?.type == 'defined') { 100 let v = subval.content(); 101 if (typeof v == 'string') 102 v = v.split(/\n/); 103 stats.defs[type.id] = v; 104 } else if (type?.definedBy && stats.defs?.[type.definedBy]?.[1]) { // hope current OIDs contain the type name (will need to parse from RFC itself) 105 try { 106 type = Defs.searchType(firstUpper(stats.defs[type.definedBy][1])); 107 } catch (e) { /*ignore*/ } 108 } 109 } 110 } 111 Defs.match(subval, type, stats); 112 } 113 } 114 return stats; 115 } 116 117} 118 119Defs.RFC = rfcdef; 120 121Defs.commonTypes = [ 122 [ 'X.509 certificate', '1.3.6.1.5.5.7.0.18', 'Certificate' ], 123 [ 'CMS / PKCS#7 envelope', '1.2.840.113549.1.9.16.0.14', 'ContentInfo' ], 124 [ 'PKCS#1 RSA private key', '1.2.840.113549.1.1.0.1', 'RSAPrivateKey' ], 125 [ 'PKCS#8 encrypted private key', '1.2.840.113549.1.8.1.1', 'EncryptedPrivateKeyInfo' ], 126 [ 'PKCS#8 private key', '1.2.840.113549.1.8.1.1', 'PrivateKeyInfo' ], 127 [ 'PKCS#10 certification request', '1.2.840.113549.1.10.1.1', 'CertificationRequest' ], 128 [ 'CMP PKI Message', '1.3.6.1.5.5.7.0.16', 'PKIMessage' ], 129].map(arr => ({ description: arr[0], ...Defs.moduleAndType(rfcdef[arr[1]], arr[2]) }));