Skip to content

Commit

Permalink
When trying to parse encapsulated values, check that the content can …
Browse files Browse the repository at this point in the history
…be parsed as well.

Closes #79 on GitHub.
  • Loading branch information
lapo-luchini committed Mar 30, 2024
1 parent 405c036 commit 8f048ac
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
12 changes: 9 additions & 3 deletions asn1.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class Stream {
let s = this.parseStringISO(start, end).str,
m = (shortYear ? reTimeS : reTimeL).exec(s);
if (!m)
return 'Unrecognized time: ' + s;
throw new Error('Unrecognized time: ' + s);
if (shortYear) {
// to avoid querying the timer, use the fixed range [1970, 2069]
// it will conform with ITU X.400 [-10, +40] sliding window until 2030
Expand Down Expand Up @@ -590,9 +590,15 @@ class ASN1 {
if (stream.get() != 0)
throw new Error('BIT STRINGs with unused bits cannot encapsulate.');
getSub();
for (let i = 0; i < sub.length; ++i)
if (sub[i].tag.isEOC())
for (let s of sub) {
if (s.tag.isEOC())
throw new Error('EOC is not supposed to be actual content.');
try {
s.content();
} catch (e) {
throw new Error('Unable to parse content: ' + e);
}
}
} catch (e) {
// but silently ignore when they don't
sub = null;
Expand Down
7 changes: 6 additions & 1 deletion dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ class ASN1DOM extends ASN1 {
}
}
head.appendChild(DOM.text(typeName));
let content = this.content(contentLength);
let content;
try {
content = this.content(contentLength);
} catch (e) {
content = 'Cannot decode: ' + e;
}
let oid;
if (content !== null) {
let preview = DOM.tag('span', 'preview'),
Expand Down
16 changes: 13 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ const tests = [
['0420041EE4E3B7ED350CC24D034E436D9A1CB15BB1E328D37062FB82E84618AB0A3C', '(32 byte)\n041EE4E3B7ED350CC24D034E436D9A1CB15BB1E328D37062FB82E84618AB0A3C', 'Do not mix encapsulated and structured octet strings'], // GitHub issue #47
['181531393835313130363231303632372E332D31323334', '1985-11-06 21:06:27.3 UTC-12:34', 'UTC offsets with minutes'], // GitHub issue #54
['181331393835313130363231303632372E332B3134', '1985-11-06 21:06:27.3 UTC+14:00', 'UTC offset +13 and +14'], // GitHub issue #54
['032100171E83C1B251803F86DD01E9CFA886BE89A7316D8372649AC2231EC669F81A84', n => { if (n.sub != null) return 'Should not decode content: ' + n.sub[0].content(); }, 'Key that resembles an UTCTime'], // GitHub issue #79
['171E83C1B251803F86DD01E9CFA886BE89A7316D8372649AC2231EC669F81A84', /^Exception:\nError: Unrecognized time: /, 'Invalid UTCTime'], // GitHub issue #79
];

let
Expand All @@ -98,17 +100,25 @@ tests.forEach(function (t) {
comment = t[2];
let result;
try {
result = ASN1.decode(Hex.decode(input)).content();
let node = ASN1.decode(Hex.decode(input));
if (typeof expected == 'function')
result = expected(node);
else
result = node.content();
//TODO: check structure, not only first level content
} catch (e) {
result = 'Exception:\n' + e;
}
if (expected instanceof RegExp)
result = expected.test(result) ? null : 'does not match';
++run;
if (result == expected) {
if (!result || result == expected) {
if (all) console.log('\x1B[1m\x1B[32mOK \x1B[39m\x1B[22m ' + comment);
} else {
++error;
console.log('\x1B[1m\x1B[31mERR\x1B[39m\x1B[22m ' + comment + '\n' + result);
console.log('\x1B[1m\x1B[31mERR\x1B[39m\x1B[22m ' + comment);
console.log(' \x1B[1m\x1B[34mEXP\x1B[39m\x1B[22m ' + expected.toString().replace(/\n/g, '\n '));
console.log(' \x1B[1m\x1B[33mGOT\x1B[39m\x1B[22m ' + result.replace(/\n/g, '\n '));
}
});
console.log(run + ' tested, ' + expErr + ' expected, ' + error + ' errors.');
Expand Down

0 comments on commit 8f048ac

Please sign in to comment.