ASN.1 encoding tutorial


Recently I in my work I had to encode an Inap (to be more precise – Sinap) content “by hand”. It’s not a rocket science however when you need to repeat this action several times it’s worth to prepare an “automatic” solution.

Input data

As an imput I have description of Furnish Charging Information in ASN.1:

Ok, how to read it? (S)INAP message FurnishChargingInformation has an argument FurnishChargingInformationArg which is eqal to FCIBillingChargingCharacteristics. FCIBillingChargingCharacteristics is a limited size octet string.

From Sinap description document I’ve found that FCIBillingChargingCharacteristics == FCIBCC.

Now it’s time to build FCIBCC module:

(save whole module as FCI-Module.asn)

What’s this? It’s a top-down definition of all elements needed to describe FCIBCC.

Building the tool

We have prepared an abstract description of FCIBCC. “Abstract” means – has no mapping to real bits and bytes yet. However, we know that in INAP protocol bytes are encoded according to BER notation. Let’s take a pen, piece of paper and spend some time to create hex output we’re looking for. Wait, this is not the way we would like to go. We’re looking for a tool 🙂

First – let’s get an ASN.1 compiler. Usually, commercial tool costs a lot of $$$. Really LOT. Lucky – I’ve found one freeware. Look for open source ANS.1 compiler. Download it and build on your platform.

Next, compile FCI-Module.asn. It should compile without errors:

An output we got a source file for our tool. Now it’s time to build the tool:

(we got several warnings but just ignore them)


Using the tool

Our goal is to prepare flow of hex numbers that’s corresponding to following XER data:

(save it as FCI.xer)

If you compare XER data with FCI-Module.asn you can easily recognise what’s going on. XER encoding is very handy human-readable format for all this ASN.1 stuff. Let’s convert it to real BER (to be more precise – DER):

When you copy these hex numbers into your IN application and put FCI message on the wire you should get something similar to:


Can you recognize highlighted numbers? Yes, yes – they are exactly the same as we got from our FCI-converter.

Let’s assume now we would like to convert hex (or more precise – BER encoded) FurnishChargingInformationArg to human readable XML.

Save highlighted element. In Wireshark I’m using right mouse button -> Copy -> Hex (Hex stream). Save it to txt file:

Now convert hex-text file into real binary:

Now we can convert BER stream into human-readable format using our hand-made tool:

Pretty cool, heh?



ASN.1 encoding tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *