Here is a project decoding the ASN.1 BER format. https://github.com/chrisridd/asn1-dump/
main logic located in this file: https://github.com/chrisridd/asn1-dump/blob/master/berd.m
And if you have enough time, itβs not difficult for you to write your own decoder after you read the standard: http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf http: //luca.ntop.org/Teaching/Appunti/asn1.html
the decoding stream looks like this: read in tags, length, sequence of values.
From the tag you will receive
- The data class, as a rule, is universal (a predefined type in the standard form, like "Logical", "Sequence" ...) and context-sensitive (to distinguish different fields of the same type).
- Primitive (e.g. boolean and integer) or Constructed (usually a sequence). because a built type can contain a primitive or built type. recursive decoding may be required.
- Tag number, define data types (boolean? Integer? Bitstring?)
Length:
- Determine the length of the content for decoding (recursive decoding may be required). Length
- has two forms (short and long). you better support both.
Value: The actual value to read at the current TLV level. if it is constructed data (for example, a sequence), the value will contain the internal TLV level.
At the end of the standard (http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf) there is an image showing a multi-level TLV, perhaps it may help you should understand BER.
After you read the standards, the best way: 1) find some GUI viewer to look at some BER certificate file to get to know it. google "ASN.1 viewer" to find. 2) start watching the code https://github.com/chrisridd/asn1-dump/blob/master/berd.m
source share