JavaScript generic ASN.1 parser (mirror)
at github-82 20 kB view raw
1#! /usr/bin/env node 2 3import * as fs from 'node:fs'; 4 5const 6 patches = { // to fix some known RFCs' ASN.1 syntax errors 7 0: [ 8 [ /\n\n[A-Z].*\n\f\n[A-Z].*\n\n/g, '' ], // page change 9 ], 10 2459: [ // currently unsupported 11 [ 'videotex (8) } (0..ub-integer-options)', 'videotex (8) }' ], 12 [ /OBJECT IDENTIFIER \( id-qt-cps \| id-qt-unotice \)/g, 'OBJECT IDENTIFIER' ], 13 [ /SIGNED \{ (SEQUENCE \{[^}]+\})\s*\}/g, 'SEQUENCE { toBeSigned $1, algorithm AlgorithmIdentifier, signature BIT STRING }' ], 14 [ /EXTENSION\.&[^,]+/g, 'OBJECT IDENTIFIER'], 15 ], 16 2986: [ // currently unsupported 17 [ /FROM (InformationFramework|AuthenticationFramework) [a-zA-Z]+/g, 'FROM $1 {joint-iso-itu-t(2) ds(5) module(1) usefulDefinitions(0) 3}' ], 18 [ /[(]v1,[^)]+[)]/g, '' ], 19 [ /[{][{][^}]+[}][}]/g, '' ], 20 [ 'SubjectPublicKeyInfo {ALGORITHM: IOSet}', 'SubjectPublicKeyInfo' ], 21 [ /PKInfoAlgorithms ALGORITHM ::=[^}]+[}]/g, '' ], 22 [ /(Attributes?) [{] ATTRIBUTE:IOSet [}]/g, '$1' ], 23 [ /CRIAttributes +ATTRIBUTE +::=[^}]+[}]/g, '' ], 24 [ /[A-Z]+[.]&id[(][{]IOSet[}][)]/g, 'OBJECT IDENTIFIER' ], 25 [ /[A-Z]+[.]&Type[(][{]IOSet[}][{]@[a-z]+[}][)]/g, 'ANY' ], 26 [ /(AlgorithmIdentifier) [{]ALGORITHM:IOSet [}]/g, '$1' ], 27 [ /SignatureAlgorithms ALGORITHM ::=[^}]+[}]/g, '' ], 28 ], 29 3161: [ // actual syntax errors 30 [ /--.*}/g, '}' ], 31 [ /^( +)--.*\n(?:\1 .*\n)+/mg, '' ], 32 [ /addInfoNotAvailable \(17\)/g, '$&,' ], 33 ], 34 5208: [ // currently unsupported 35 [ 'FROM InformationFramework informationFramework', 'FROM InformationFramework {joint-iso-itu-t(2) ds(5) module(1) usefulDefinitions(0) 3}' ], 36 [ ' {{PrivateKeyAlgorithms}}', '' ], 37 [ 'Version ::= INTEGER {v1(0)} (v1,...)', 'Version ::= INTEGER {v1(0)}' ], 38 [ ' {{KeyEncryptionAlgorithms}}', '' ], 39 [ /\.\.\. -- For local profiles/g, '' ], 40 ], 41 5280: [ // currently unsupported 42 [ 'videotex (8) } (0..ub-integer-options)', 'videotex (8) }' ], 43 [ /OBJECT IDENTIFIER \( id-qt-cps \| id-qt-unotice \)/g, 'OBJECT IDENTIFIER' ], 44 ], 45 4210: [ 46 [ /^\s+-- .*\r?\n/mg, '' ], // comments 47 ], 48 8017: [ // this RFC uses a lot of currently unsupported syntax 49 [ /ALGORITHM-IDENTIFIER ::= CLASS[^-]+--/, '--' ], 50 [ /\n +\S+ +ALGORITHM-IDENTIFIER[^\n]+(\n [^\n]+)+\n [}]/g, '' ], 51 [ /AlgorithmIdentifier [{] ALGORITHM-IDENTIFIER:InfoObjectSet [}] ::=(\n [^\n]+)+\n [}]/, 'AlgorithmIdentifier ::= ANY'], 52 [ /algorithm +id-[^,\n]+,/g, 'algorithm ANY,' ], 53 [ / (sha1 HashAlgorithm|mgf1SHA1 MaskGenAlgorithm|pSpecifiedEmpty PSourceAlgorithm|rSAES-OAEP-Default-Identifier RSAES-AlgorithmIdentifier|rSASSA-PSS-Default-Identifier RSASSA-AlgorithmIdentifier) ::= [{](\n( [^\n]+)?)+\n [}]/g, '' ], 54 [ / ::= AlgorithmIdentifier [{]\s+[{][^}]+[}]\s+[}]/g, ' ::= AlgorithmIdentifier' ], 55 [ /OCTET STRING[(]SIZE[(]0..MAX[)][)]/g, 'OCTET STRING' ], 56 [ /emptyString EncodingParameters ::= ''H/g, '' ], 57 [ /[(]CONSTRAINED BY[^)]+[)]/g, '' ], 58 ], 59 }; 60 61// const reWhitespace = /(?:\s|--(?:[}-]?[^\n}-])*(?:\n|--))*/y; 62const reWhitespace = /(?:\s|--(?:-?[^\n-])*(?:\n|--))*/my; 63const reIdentifier = /[a-zA-Z](?:[-]?[a-zA-Z0-9])*/y; 64const reNumber = /0|[1-9][0-9]*/y; 65const reToken = /[(){},[\];]|::=|OPTIONAL|DEFAULT|NULL|TRUE|FALSE|\.\.|OF|SIZE|MIN|MAX|DEFINED BY|DEFINITIONS|TAGS|BEGIN|EXPORTS|IMPORTS|FROM|END/y; 66const reType = /ANY|BOOLEAN|INTEGER|(?:BIT|OCTET)\s+STRING|OBJECT\s+IDENTIFIER|SEQUENCE|SET|CHOICE|ENUMERATED|(?:Generalized|UTC)Time|(?:BMP|General|Graphic|IA5|ISO64|Numeric|Printable|Teletex|T61|Universal|UTF8|Videotex|Visible)String/y; 67const reTagClass = /UNIVERSAL|APPLICATION|PRIVATE|/y; 68const reTagType = /IMPLICIT|EXPLICIT|/y; 69const reTagDefault = /(AUTOMATIC|IMPLICIT|EXPLICIT) TAGS|/y; 70 71let asn1; 72let currentMod; 73 74function searchImportedValue(id) { 75 for (let imp of Object.values(currentMod.imports)) 76 for (let name of imp.types) 77 if (name == id) { 78 if (!(imp.oid in asn1)) 79 throw new Error('Cannot find module: ' + imp.oid + ' ' + id); 80 if (id in asn1[imp.oid].values) 81 return asn1[imp.oid].values[id]; 82 throw new Error('Cannot find imported value: ' + imp.oid + ' ' + id); 83 } 84 throw new Error('Cannot find imported value in any module: ' + id); 85} 86 87class Parser { 88 constructor(enc, pos) { 89 this.enc = enc; 90 this.pos = pos; 91 this.start = pos; 92 } 93 getChar(pos) { 94 if (pos === undefined) 95 pos = this.pos++; 96 if (pos >= this.enc.length) 97 throw 'Requesting byte offset ' + pos + ' on a stream of length ' + this.enc.length; 98 return this.enc.charAt(pos); 99 } 100 exception(s) { 101 const pos = this.pos; 102 let from = Math.max(pos - 30, this.start); 103 let to = Math.min(pos + 30, this.enc.length); 104 let ctx = ''; 105 let arrow = ''; 106 let i = from; 107 for (; i < pos; ++i) { 108 ctx += this.getChar(i); 109 arrow += ' '; 110 } 111 ctx += this.getChar(i++); 112 arrow += '^'; 113 for (; i < to; ++i) 114 ctx += this.getChar(i); 115 // calculate line/column 116 let line = 1; 117 let lastLF = 0; 118 for (let i = 0; i < pos; ++i) 119 if (this.enc.charAt(i) == '\n') { 120 ++line; 121 lastLF = i; 122 } 123 let column = pos - lastLF; 124 throw new Error('[position ' + pos + ', line ' + line + ':' + column + '] ' + s + '\n' + ctx.replace(/\s/g, ' ') + '\n' + arrow); 125 } 126 peek() { 127 return this.enc.charCodeAt(this.pos); 128 } 129 peekChar() { 130 return this.enc.charAt(this.pos); 131 } 132 isWhitespace() { 133 let c = this.peekChar(); 134 return c == ' ' || c == '\n'; 135 } 136 isDigit() { 137 let c = this.peekChar(); 138 return c >= '0' && c <= '9'; 139 } 140 skipWhitespace() { 141 reWhitespace.lastIndex = this.pos; 142 let s = reWhitespace.exec(this.enc); 143 if (s) 144 this.pos = reWhitespace.lastIndex; 145 } 146 // DefStream.prototype.eat = function (str) { 147 // for (let i = 0; i < str.length; ++i) { 148 // let c = this.getChar(); 149 // if (c != str.charAt(i)) 150 // throw new Error("Found '" + c + "', was expecting '" + str.charAt(i) + "'"); 151 // } 152 // }; 153 getRegEx(type, re) { 154 this.skipWhitespace(); 155 re.lastIndex = this.pos; 156 let s = re.exec(this.enc); //TODO: does not work with typed arrays 157 if (!s) 158 this.exception("Found '" + this.peekChar() + "', was expecting a " + type); 159 s = s[0]; 160 // console.log('[debug] getRexEx@' + this.pos + ' = ' + s); 161 this.pos = re.lastIndex; 162 this.skipWhitespace(); 163 return s; 164 } 165 parseIdentifier() { 166 let id = this.getRegEx('identifier', reIdentifier); 167 // console.log('[debug] parseIdentifier = ' + id); 168 return id; 169 } 170 parseNumber() { 171 let id = this.getRegEx('number', reNumber); 172 // console.log('[debug] parseNumber = ' + id); 173 return id; 174 } 175 parseToken() { 176 let tok = this.getRegEx('token', reToken); 177 return tok; 178 } 179 tryToken(expect) { 180 let p = this.pos; 181 let t; 182 try { t = this.parseToken(); } catch (e) { /*ignore*/ } 183 // console.log('[debug] tryToken(' + expect + ') = ' + t); 184 if (t == expect) 185 return true; 186 else { 187 this.pos = p; 188 return false; 189 } 190 } 191 expectToken(expect) { 192 let p = this.pos; 193 let t; 194 try { t = this.parseToken(); } 195 catch (e) { console.log('[debug] expectToken', e); } 196 // console.log('[debug] expectToken(' + expect + ') = ' + t); 197 if (t != expect) { 198 this.pos = p; 199 this.exception("Found '" + t + "', was expecting '" + expect + "'"); 200 } 201 } 202 parseNumberOrValue() { 203 if (this.isDigit()) 204 return +this.parseNumber(); 205 return this.parseIdentifier(); 206 } 207 parseRange() { 208 let min = this.tryToken('MIN') ? 'MIN' : this.parseNumberOrValue(); 209 if (this.tryToken('..')) { 210 let max = this.tryToken('MAX') ? 'MAX' : this.parseNumberOrValue(); 211 return [min, max]; 212 } 213 return min; 214 } 215 parseBuiltinType() { 216 let x = { 217 name: this.getRegEx('type', reType), 218 type: 'builtin', 219 }; 220 // console.log('[debug] parseType = ' + x.name); 221 try { 222 switch (x.name) { 223 case 'ANY': 224 if (this.tryToken('DEFINED BY')) 225 x.definedBy = this.parseIdentifier(); 226 break; 227 case 'BOOLEAN': 228 case 'OCTET STRING': 229 case 'OBJECT IDENTIFIER': 230 break; 231 case 'CHOICE': 232 x.content = this.parseElementTypeList(); 233 break; 234 case 'SEQUENCE': 235 case 'SET': 236 if (this.peekChar() == '{') { 237 x.content = this.parseElementTypeList(); 238 } else { 239 x.typeOf = 1; 240 if (this.tryToken('SIZE')) { 241 this.expectToken('('); 242 x.size = this.parseRange(); 243 this.expectToken(')'); 244 } 245 this.expectToken('OF'); 246 x.content = [this.parseType()]; 247 } 248 break; 249 case 'INTEGER': 250 if (this.tryToken('(')) { 251 x.range = this.parseRange(); 252 this.expectToken(')'); 253 } 254 // falls through 255 case 'ENUMERATED': 256 case 'BIT STRING': 257 if (this.tryToken('{')) { 258 x.content = {}; 259 do { 260 let id = this.parseIdentifier(); 261 this.expectToken('('); 262 let val = this.parseNumber(); //TODO: signed 263 this.expectToken(')'); 264 x.content[id] = +val; 265 } while (this.tryToken(',')); 266 this.expectToken('}'); 267 } 268 break; 269 case 'BMPString': 270 case 'GeneralString': 271 case 'GraphicString': 272 case 'IA5String': 273 case 'ISO646String': 274 case 'NumericString': 275 case 'PrintableString': 276 case 'TeletexString': 277 case 'T61String': 278 case 'UniversalString': 279 case 'UTF8String': 280 case 'VideotexString': 281 case 'VisibleString': 282 if (this.tryToken('(')) { 283 if (this.tryToken('SIZE')) { 284 this.expectToken('('); 285 x.size = this.parseRange(); 286 this.expectToken(')'); 287 } 288 this.expectToken(')'); 289 } 290 break; 291 default: 292 x.content = 'TODO:unknown'; 293 } 294 } catch (e) { 295 console.log('[debug] parseBuiltinType content', e); 296 x.content = 'TODO:exception'; 297 } 298 return x; 299 } 300 parseTaggedType() { 301 this.expectToken('['); 302 let tagClass = this.getRegEx('class', reTagClass) || 'CONTEXT'; //TODO: use module defaults 303 let t = this.parseNumber(); 304 this.expectToken(']'); 305 let plicit = this.getRegEx('explicit/implicit', reTagType); 306 if (plicit == '') plicit = currentMod.tagDefault; 307 let x = this.parseType(); 308 return { 309 name: '[' + t + ']', 310 type: 'tag', 311 'class': tagClass, 312 explicit: (plicit == 'EXPLICIT'), 313 content: [{ name: '', type: x }], 314 }; 315 } 316 parseType() { 317 if (this.peekChar() == '[') 318 return this.parseTaggedType(); 319 let p = this.pos; 320 try { 321 return this.parseBuiltinType(); 322 } catch (e) { 323 // console.log('[debug] parseAssignment failed on parseType', e); 324 this.pos = p; 325 let x = { 326 name: this.parseIdentifier(), 327 type: 'defined', 328 }; 329 // let from = searchImportedType(x.name); 330 // if (from) 331 // x.module = from; 332 return x; 333 //TODO "restricted string type" 334 } 335 } 336 parseValueBoolean() { 337 let p = this.pos; 338 let t = this.parseToken(); 339 if (t == 'TRUE') 340 return true; 341 if (t == 'FALSE') 342 return false; 343 this.pos = p; 344 this.exception("Found '" + t + "', was expecting a boolean"); 345 } 346 parseValueOID() { 347 this.expectToken('{'); 348 let v = ''; 349 while (!this.tryToken('}')) { 350 let p = this.pos; 351 let val; 352 if (this.isDigit()) 353 val = this.parseNumber(); 354 else { 355 this.pos = p; 356 let id = this.parseIdentifier(); 357 if (this.tryToken('(')) { 358 val = this.parseNumber(); 359 this.expectToken(')'); 360 } else { 361 if (id in currentMod.values) // defined in local module 362 val = currentMod.values[id].value; 363 else try { 364 val = searchImportedValue(id); 365 } catch (e) { 366 this.exception(e.message); 367 } 368 } 369 } 370 if (v.length) v += '.'; 371 v += val; 372 } 373 return v; 374 } 375 parseValue() { 376 let c = this.peekChar(); 377 if (c == '{') 378 return this.parseValueOID(); 379 if (c >= '0' && c <= '9') 380 return +this.parseNumber(); 381 if (c == '-') 382 return -this.parseNumber(); 383 let p = this.pos; 384 try { 385 switch (this.parseToken()) { 386 case 'TRUE': 387 return true; 388 case 'FALSE': 389 return false; 390 case 'NULL': 391 return null; 392 } 393 } catch (e) { 394 this.pos = p; 395 } 396 p = this.pos; 397 try { 398 return this.parseIdentifier(); 399 } catch (e) { 400 this.pos = p; 401 } 402 this.exception('Unknown value type.'); 403 } 404 /*DefStream.prototype.parseValue = function (type) { 405 console.log('[debug] parseValue type:', type); 406 if (type.type == 'defined') { 407 if (!(type.name in types)) 408 this.exception("Missing type: " + type.name); 409 type = types[type.name]; 410 } 411 switch (type.name) { 412 case 'BOOLEAN': 413 return this.parseValueBoolean(); 414 case 'OBJECT IDENTIFIER': 415 return this.parseValueOID(); 416 default: 417 console.log('[debug] parseValue unknown:', type); 418 return 'TODO:value'; 419 } 420 }*/ 421 parseElementType() { 422 let x = Object.assign({ id: this.parseIdentifier() }, this.parseType()); 423 // console.log('[debug] parseElementType 1:', x); 424 if (this.tryToken('OPTIONAL')) 425 x.optional = true; 426 if (this.tryToken('DEFAULT')) 427 x.default = this.parseValue(x.type); 428 // console.log('[debug] parseElementType 2:', x); 429 return x; 430 } 431 parseElementTypeList() { 432 let v = []; 433 this.expectToken('{'); 434 do { 435 v.push(this.parseElementType()); 436 } while (this.tryToken(',')); 437 this.expectToken('}'); 438 return v; 439 } 440 parseAssignment() { 441 let name = this.parseIdentifier(); 442 if (this.tryToken('::=')) { // type assignment 443 // console.log('type name', name); 444 let type = this.parseType(); 445 currentMod.types[name] = { name, type }; 446 return currentMod.types[name]; 447 } else { // value assignment 448 // console.log('value name', name); 449 let type = this.parseType(); 450 // console.log('[debug] parseAssignment type:', type); 451 this.expectToken('::='); 452 let value = this.parseValue(type); 453 currentMod.values[name] = { name, type, value }; 454 return currentMod.values[name]; 455 } 456 } 457 parseModuleIdentifier() { 458 return { 459 name: this.parseIdentifier(), 460 oid: this.parseValueOID(), 461 }; 462 } 463 parseSymbolsImported() { 464 let imports = {}; 465 do { 466 let l = []; 467 do { 468 l.push(this.parseIdentifier()); 469 } while (this.tryToken(',')); 470 this.expectToken('FROM'); 471 let mod = this.parseModuleIdentifier(); 472 mod.types = l; 473 imports[mod.oid] = mod; 474 } while (this.peekChar() != ';'); 475 return imports; 476 } 477 parseModuleDefinition(file) { 478 let mod = this.parseModuleIdentifier(); 479 currentMod = mod; // for deeply nested parsers 480 mod.source = file; 481 this.expectToken('DEFINITIONS'); 482 mod.tagDefault = this.getRegEx('tag default', reTagDefault).split(' ')[0]; 483 this.expectToken('::='); 484 this.expectToken('BEGIN'); 485 //TODO this.tryToken('EXPORTS') 486 if (this.tryToken('IMPORTS')) { 487 mod.imports = this.parseSymbolsImported(); 488 this.expectToken(';'); 489 } 490 mod.values = {}; 491 mod.types = {}; 492 while (!this.tryToken('END')) 493 this.parseAssignment(); 494 return mod; 495 } 496} 497 498let s = fs.readFileSync(process.argv[2], 'utf8'); 499let num = /^Request for Comments: ([0-9]+)/m.exec(s)[1]; 500console.log('RFC:', num); 501for (let p of patches[0]) 502 s = s.replace(p[0], p[1]); 503if (num in patches) 504 for (let p of patches[num]) 505 s = s.replace(p[0], p[1]); 506fs.writeFileSync(process.argv[2].replace(/[.]txt$/, '_patched.txt'), s, 'utf8'); 507// console.log(s); 508asn1 = JSON.parse(fs.readFileSync(process.argv[3], 'utf8')); 509const reModuleDefinition = /\s[A-Z](?:[-]?[a-zA-Z0-9])*\s*\{[^}]+\}\s*(^--.*|\n)*DEFINITIONS/gm; 510let m; 511while ((m = reModuleDefinition.exec(s))) { 512 new Parser(s, m.index).parseModuleDefinition(process.argv[2]); 513 console.log('Module:', currentMod.name); 514 // fs.writeFileSync('rfc' + num + '.json', JSON.stringify(currentMod, null, 2) + '\n', 'utf8'); 515 asn1[currentMod.oid] = currentMod; 516} 517/*asn1 = Object.keys(asn1).sort().reduce( 518 (obj, key) => { 519 obj[key] = asn1[key]; 520 return obj; 521 }, 522 {} 523);*/ 524fs.writeFileSync(process.argv[3], JSON.stringify(asn1, null, 2) + '\n', 'utf8'); 525// console.log('Module:', mod); 526/*while ((idx = s.indexOf('::=', idx + 1)) >= 0) { 527 let line = s.lastIndexOf('\n', idx) + 1; 528 // console.log('[line] ' + s.slice(line, line+30)); 529 try { 530 let a = new DefStream(s, line).parseAssignment(); 531 // console.log('[assignment]', util.inspect(a, {showHidden: false, depth: null, colors: true})); 532 } catch (e) { 533 console.log('Error:', e); 534 } 535}*/ 536console.log('Done.');