Find and replace (increment) ASN.1 BER hex value

489 Views Asked by At

I have a long string of hex (converted from BER ASN.1) where I need to find and increment a particular value which is incorrect.

<TAG> <LENGTH> <VALUE to INCREMENT>

the ASN.1 tag is 84 and the length byte will change from 01 to 02 when the value > 127dec. And the value to increment will therefore become 2 bytes.

The value should start at 00.

e.g.

- Original file: ...840101...840107...84020085...84020097
- New file:      ...840100...840101...84020080...84020081

Any ideas how best to do this, preferably using standard bash commands?

2

There are 2 best solutions below

0
On

Ilya Etingof hinted at this already, but to be explicit about it, BER uses TAG, LENGTH, VALUE (TLV) encoding, where the VALUE can itself be a TLV. If you change the length in a TLV that is nested inside a TLV, you will need to update all of the lengths of the enclosing TLVs as well. It is not a simple search/replace operation.

1
On

Assuming you have the octet-stream in text work already, you may consider searching/replacing pieces of text with awk or sed. If you can only use bash, may be variable substitution (${parameter/pattern/string} or ${parameter:offset:length}) would work?

Keep in mind however, that BER is quite flexible in the sense that (sometimes) the same data structure may be encoded differently and that would still constitute a valid encoding. The rationale behind that is to allow the encoder to optimize for its very own situation (e.g. save on memory or CPU cycles or on copying etc).

What I am trying to say that depending on your situation there may be a chance that your search/replace logic may fail. The bullet-proof solution would be to fully decode your BER octet stream, change the data structure you need and re-encode it back into BER.