Beginner question - how to parse ASN.1 structures in C#

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Beginner question - how to parse ASN.1 structures in C#

ziggi slaw
Consider this input: 04133011020100310C130141130143130149130150

It is a DerOctetString, with length of 13h and a value. The value itself is also an ASN.1 structure:
30 11
    02 01 00
    31 0C
        13 01 41
        13 01 43
        13 01 49
        13 01 50 

How can I get the 'final' PrintableString values, in this case 41, 43, 49 and 50?

I guess first I have to convert input byte array to DerOctetString.... This is easy, 'var s = new DerOctetString(input);'. Then I should probably do something like this:
    - get the value from the DerOctetString
    - generate new DerSequence from the value
    - get the DerSet from the DerSequence
    - get all PrintableString values from the sequence.

I stumble right at the beginning - how to you even get the value of of the DerOctetString? ToString method returns tag, length and value. GetOctets method returns all bytes, including tag and length, ...
Off course I could use offset (read from index 3 onwards), but there's probably a more idiomatic way to do it Could anyone help with an example?

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Beginner question - how to parse ASN.1 structures in C#

Peter Dettman-3
Hi ziggi,
Something like this to parse this ASN.1 value:

            byte[] data =
Hex.DecodeStrict("04133011020100310C130141130143130149130150");
            Asn1Object obj = Asn1Object.FromByteArray(data);
            Asn1OctetString oct = Asn1OctetString.GetInstance(obj);
            Asn1Sequence seq = Asn1Sequence.GetInstance(oct.GetOctets());
            DerInteger num = DerInteger.GetInstance(seq[0]);
            Asn1Set set = Asn1Set.GetInstance(seq[1]);
            foreach (Asn1Encodable e in set)
            {
                DerPrintableString str = DerPrintableString.GetInstance(e);
                Console.WriteLine(str.GetString());
            }

However generally you will need to check the actual ASN.1 syntax
definition; there could be optional fields or other variations that a
parser for this type would need to handle.

Regards,
Pete Dettman

On 24/6/20 5:34 pm, ziggi slaw wrote:

> Consider this input: 04133011020100310C130141130143130149130150
>
> It is a DerOctetString, with length of 13h and a value. The value itself
> is also an ASN.1 structure:
> 30 11
>     02 01 00
>     31 0C
>         13 01 41
>         13 01 43
>         13 01 49
>         13 01 50 
>
> How can I get the 'final' PrintableString values, in this case 41, 43,
> 49 and 50?
>
> I guess first I have to convert input byte array to DerOctetString....
> This is easy, 'var s = new DerOctetString(input);'. Then I should
> probably do something like this:
>     - get the value from the DerOctetString
>     - generate new DerSequence from the value
>     - get the DerSet from the DerSequence
>     - get all PrintableString values from the sequence.
>
> I stumble right at the beginning - how to you even get the value of of
> the DerOctetString? ToString method returns tag, length and value.
> GetOctets method returns all bytes, including tag and length, ...
> Off course I could use offset (read from index 3 onwards), but there's
> probably a more idiomatic way to do it Could anyone help with an example?
>
> Thanks!