Skip to content

Commit b12e6e6

Browse files
committed
Add 'Wrap Input in SEQUENCE' to Parse ASN.1 hex string
This will parse extra tags concatenated to the first tag.
1 parent d635cca commit b12e6e6

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/core/operations/ParseASN1HexString.mjs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ class ParseASN1HexString extends Operation {
3434
"name": "Truncate octet strings longer than",
3535
"type": "number",
3636
"value": 32
37+
},
38+
{
39+
"name": "Wrap Input in SEQUENCE",
40+
"type": "boolean",
41+
"value": false,
42+
"hint": "Use this when there is extra data that needs to be decoded"
3743
}
3844
];
3945
}
@@ -44,8 +50,34 @@ class ParseASN1HexString extends Operation {
4450
* @returns {string}
4551
*/
4652
run(input, args) {
47-
const [index, truncateLen] = args;
48-
return r.ASN1HEX.dump(input.replace(/\s/g, "").toLowerCase(), {
53+
const [index, truncateLen, addSequence] = args;
54+
let hex = input.replace(/\s/g, "").toLowerCase();
55+
if (addSequence) {
56+
let sequence = '30';
57+
let len = hex.length / 2;
58+
if (len <= 127) {
59+
// We can use the short form
60+
sequence += len.toString(16).padStart(2, '0');
61+
} else {
62+
let bytes = 0;
63+
let encoded = '';
64+
// Calculate the number of bytes needed to encode the length
65+
while (len > 0) {
66+
bytes++;
67+
// While we are at it, also build up the length
68+
encoded = (len & 0xff).toString(16).padStart(2, '0') + encoded;
69+
len >>= 8;
70+
}
71+
// encode the number of bytes needed for the length
72+
sequence += (bytes | 0x80).toString(16).padStart(2, '0');
73+
// add the encoded length
74+
sequence += encoded;
75+
}
76+
// Add the sequence + length in front of the original input
77+
hex = sequence + hex;
78+
}
79+
80+
return r.ASN1HEX.dump(hex, {
4981
"ommit_long_octet": truncateLen
5082
}, index);
5183
}

0 commit comments

Comments
 (0)