A Jackson inspired annotation based parser for Asn.1 BER data. Decode or encode your POJOs with annotations.
Most of the Asn.1 parser I have seen online were either using their specific classes to decode data (i.e. AsnInteger instead of using regular java int or Integer) or they came with a generator which converted Asn specifications into obscure code riddled with their internals.
This parser allows selective encoding/decoding, meaning that you don’t have to read all Asn.1 fields into your data, but selectively choose which fields to decode.
For an example, take a look at the following specification:
FooProtocol DEFINITIONS ::= BEGIN
FooQuestion ::= SEQUENCE {
trackingNumber INTEGER,
question IA5String
}
FooAnswer ::= SEQUENCE {
questionNumber INTEGER,
answer BOOLEAN
}
END
If you map your POJO properly, you can only decode 'answer' without decoding 'question' if you are not interested in it.
Add the JitPack repository into your pom.xml
.
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add the following under your <dependencies>
:
<dependencies>
<dependency>
<groupId>com.github.alturkovic</groupId>
<artifactId>asn-parser</artifactId>
<version>[insert latest version here]</version>
</dependency>
</dependencies>
This is an example of a complex POJO class using @AsnX
annotations to decode BER data and @AsnPostProcessMethod("postDecode")
to call a custom method to further configure
a decoded object.
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@AsnPostProcessMethod("postDecode")
@AsnStructure(@AsnTag(value = 16, type = Type.PRIVATE))
public class Person {
@AsnPrimitive
private boolean male;
@AsnPrimitive
private Integer age;
@AsnCollection(elementTag = @AsnTag(5), structured = false, asnConverter = HexStringConverter.class, type = String.class)
private Set<String> phones;
@AsnCollection(value = @AsnTag(1), type = Address.class)
private List<Address> addresses;
private boolean adult;
public Person(final boolean male, final int age, final Set<String> phones) {
this.male = male;
this.age = age;
this.phones = phones;
}
private void postDecode() {
adult = age >= 18;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Address {
@AsnPrimitive
private String street;
@AsnPrimitive
private int number;
@AsnPrimitive(@AsnTag(2))
private boolean enabled;
}
}
Example of BER data representing a possible Person
object.
F0390101FF020118311085063859980690038506385998069002A11F300D040546697273740201018201FF300E04065365636F6E64020102820100
We can simply decode this data by calling Person person = asnDecoder.decode(Person.class, byteArrayInputStream);
Only BER rules are supported.
There is no code generation tool, you will need to map the POJOs manually.
This parser reads the data as it is without validations, meaning that if the TLV structure is valid, it will be decoded/encoded. There is (currently) no support for any validations, such as SIZE(0..100)
.
-
More implementations (DER, PER, XER, …)
-
JavaDoc and documentation improvements
-
Class generator?
-
Asn validations (i.e. SIZE)