Łukasz Chorąży Łukasz Chorąży - 1 year ago 173
Java Question

Parsing ASN1 data

I'm supposed to parse ASN1 encoded byte arrays to a Java object. My company gave me a documentation with very detailed structures. However in their example of parsing data (no code, just mapping from bits to values):


  1. Converting last 7 bytes from HEX to a binary

  2. Assigning values based on specific bits in those 7 bytes



There is no consideration for tags and lengths. The example also just skips the proceeding bytes. I'm not sure how can I parse data like that, using Bouncy Castle ASN1 library.

This is the data:

Example data

And this is the mapping:

22 0....... some field = 0
.01000.. some field = 8
......1. some field = 1
.......0 some field = 0


Does the ASN1 encoded data have to be in Tag-Length-Value format or can it be using some kind of specific grammar?

Right now this is my code:

ASN1InputStream asn1InputStream = new ASN1InputStream(
new ByteArrayInputStream(
ByteArrayUtil.hexStringToByteArray(
"16" + "07" +
"22" + "00" + "00" + "19" + "BF" + "12" + "E0")));
ASN1Primitive deria5String;
while ((deria5String = asn1InputStream.readObject()) != null) {
//process
}


Parsing those last 7 bytes gives me object of type DERIA5String.

Edit 1: Here is the definition of ASN1 structures I should be getting reading the InputStream:
Data definition

And this is the example of reading InputStream that I have in documentation:
Example of parsing

Answer Source

Does the ASN1 encoded data have to be in Tag-Length-Value format or can it be using some kind of specific grammar?

BER and DER encoded data use TLV format. However, that doesn't mean that some octet string, integer, character string, etc. isn't given some special meaning, not represented in ASN.1, by your application. If I were to hazard a guess, since "16 07" corresponds to a tag-length of [UNIVERSAL 22]-7, where that particular tag indicates a character string, I would guess they are parsing some data out of a character string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download