JavaScript generic ASN.1 parser (mirror)
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add new tests to avoid regressions on type recognition.

+47 -2
+10
.vscode/launch.json
··· 7 7 { 8 8 "type": "node", 9 9 "request": "launch", 10 + "name": "test", 11 + "skipFiles": [ 12 + "<node_internals>/**" 13 + ], 14 + "program": "${workspaceFolder}/test.js", 15 + "args": [] 16 + }, 17 + { 18 + "type": "node", 19 + "request": "launch", 10 20 "name": "dumpASN1", 11 21 "skipFiles": [ 12 22 "<node_internals>/**"
+10
examples/pkcs1.pem.dump
··· 1 + RSAPrivateKey SEQUENCE @0+605 (constructed): (9 elem) 2 + version Version INTEGER @4+1: 0 3 + modulus INTEGER @7+129: (1024 bit)|117127183230921204401013393277767517103803021018182691416238029726599496957198017684901559383009459523071384847726671610377559004781710040051270748910270447756432733966622455162759462672603884318432883204215291756888413811504344870556870978773843838353155520698675084344179957236491745093327422201227604749459 4 + publicExponent INTEGER @139+3: 65537 5 + privateExponent INTEGER @144+129: (1024 bit)|92276282475226568589241550905865273709561889943250510752753436239739158443231274686159532997597455087892667385718350070754140814118578097684166368734227728473322161416664690885212698653195373932779792506838124961141214987311742627151011567342689730655630521241854903774681567286735997541758916977964830429313 6 + prime1 INTEGER @276+65: (512 bit)|10896821561662485361386011233938116142526936125648167362054539174681694465821232677159478832161550957167298418313068706031799276527005193877616679381489381 7 + prime2 INTEGER @343+65: (512 bit)|10748747473575365133625210720374663113238804836624829882840358384492696273600075746246298849279878235356215808273663889910189484797595139778277124898735639 8 + exponent1 INTEGER @410+64: (511 bit)|6091625365131581796315047890165719534213640521161662994088715561328917102465668272778610952193459304175325574129665657306361751287362700277617869028176853 9 + exponent2 INTEGER @476+65: (512 bit)|9619864112105977791898517014707043200694399482542575521432448550956476604540013165392532656448448632323473488540572223305800601817570919153380021725291669 10 + coefficient INTEGER @543+64: (511 bit)|4162468593383244686217782691133418477400302049612992866028022118893358867845916127581019785866484075341057799618361552573853678007698434146811407259880551
+2 -2
index.js
··· 86 86 if (area.value === '') area.value = Base64.pretty(b64); 87 87 try { 88 88 window.location.hash = hash = '#' + b64; 89 - } catch (e) { 89 + } catch (ignore) { 90 90 // fails with "Access Denied" on IE with URLs longer than ~2048 chars 91 91 window.location.hash = hash = '#'; 92 92 } ··· 122 122 else if (Base64.re.test(str)) der = Base64.unarmor(str); 123 123 else der = str; 124 124 decode(der); 125 - } catch (e) { 125 + } catch (ignore) { 126 126 text(tree, 'Cannot decode file.'); 127 127 dump.innerHTML = ''; 128 128 }
+25
test.js
··· 1 1 #!/usr/bin/env node 2 2 3 + import * as fs from 'node:fs'; 3 4 import { ASN1, Stream } from './asn1.js'; 5 + import { Defs } from './defs.js'; 4 6 import { Hex } from './hex.js'; 5 7 import { Base64 } from './base64.js'; 6 8 import { Int10 } from './int10.js'; ··· 168 170 ['181331393835313130363231303632372E332B3134', '1985-11-06 21:06:27.3 UTC+14:00', 'UTC offset +13 and +14'], // GitHub issue #54 169 171 ['032100171E83C1B251803F86DD01E9CFA886BE89A7316D8372649AC2231EC669F81A84', n => { if (n.sub != null) return 'Should not decode content: ' + n.sub[0].content(); }, 'Key that resembles an UTCTime'], // GitHub issue #79 170 172 ['171E83C1B251803F86DD01E9CFA886BE89A7316D8372649AC2231EC669F81A84', /^Exception:\nError: Unrecognized time: /, 'Invalid UTCTime'], // GitHub issue #79 173 + ])); 174 + 175 + tests.push(new Tests(function () { 176 + const examples = fs.readdirSync('examples/').filter(f => f.endsWith('.dump')); 177 + for (const example of examples) { 178 + const filename = example.slice(0, -5); // Remove '.dump' suffix 179 + const expected = fs.readFileSync('examples/' + example, 'utf8'); 180 + let data = fs.readFileSync('examples/' + filename); 181 + if (filename.endsWith('.pem')) 182 + data = Base64.unarmor(data); 183 + let node = ASN1.decode(data); 184 + const types = Defs.commonTypes 185 + .map(type => { 186 + const stats = Defs.match(node, type); 187 + return { type, match: stats.recognized / stats.total }; 188 + }) 189 + .sort((a, b) => b.match - a.match); 190 + Defs.match(node, types[0].type); 191 + let result = node.toPrettyString(); 192 + this.checkResult(result, expected, 'Example: ' + filename); 193 + } 194 + }, [ 195 + [0], 171 196 ])); 172 197 173 198 tests.push(new Tests(function (t) {