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