Skip to content

Commit

Permalink
Add 'Wrap Input in SEQUENCE' to Parse ASN.1 hex string
Browse files Browse the repository at this point in the history
This will parse extra tags concatenated to the first tag.
  • Loading branch information
learn-more committed Aug 23, 2024
1 parent d635cca commit b12e6e6
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/core/operations/ParseASN1HexString.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class ParseASN1HexString extends Operation {
"name": "Truncate octet strings longer than",
"type": "number",
"value": 32
},
{
"name": "Wrap Input in SEQUENCE",
"type": "boolean",
"value": false,
"hint": "Use this when there is extra data that needs to be decoded"
}
];
}
Expand All @@ -44,8 +50,34 @@ class ParseASN1HexString extends Operation {
* @returns {string}
*/
run(input, args) {
const [index, truncateLen] = args;
return r.ASN1HEX.dump(input.replace(/\s/g, "").toLowerCase(), {
const [index, truncateLen, addSequence] = args;
let hex = input.replace(/\s/g, "").toLowerCase();
if (addSequence) {
let sequence = '30';

Check failure on line 56 in src/core/operations/ParseASN1HexString.mjs

View workflow job for this annotation

GitHub Actions / main

Strings must use doublequote
let len = hex.length / 2;
if (len <= 127) {
// We can use the short form
sequence += len.toString(16).padStart(2, '0');

Check failure on line 60 in src/core/operations/ParseASN1HexString.mjs

View workflow job for this annotation

GitHub Actions / main

Strings must use doublequote
} else {
let bytes = 0;
let encoded = '';

Check failure on line 63 in src/core/operations/ParseASN1HexString.mjs

View workflow job for this annotation

GitHub Actions / main

Strings must use doublequote
// Calculate the number of bytes needed to encode the length
while (len > 0) {
bytes++;
// While we are at it, also build up the length
encoded = (len & 0xff).toString(16).padStart(2, '0') + encoded;

Check failure on line 68 in src/core/operations/ParseASN1HexString.mjs

View workflow job for this annotation

GitHub Actions / main

Strings must use doublequote
len >>= 8;
}
// encode the number of bytes needed for the length
sequence += (bytes | 0x80).toString(16).padStart(2, '0');

Check failure on line 72 in src/core/operations/ParseASN1HexString.mjs

View workflow job for this annotation

GitHub Actions / main

Strings must use doublequote
// add the encoded length
sequence += encoded;
}
// Add the sequence + length in front of the original input
hex = sequence + hex;
}

return r.ASN1HEX.dump(hex, {
"ommit_long_octet": truncateLen
}, index);
}
Expand Down

0 comments on commit b12e6e6

Please sign in to comment.