Beginner question - how to create custom ASN.1 structure in C#

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

Beginner question - how to create custom ASN.1 structure in C#

ziggi slaw
Beginner question - how to create custom ASN.1 structure in C#

I have to create a custom ASN.1 structure, currently I use my own Tlv object, but would like to know how to do it with Bouncy Castle.

An example:
    var x = new Tlv(0x7f21, new List<Tlv>
    {
        new Tlv(0x7f4e, new List<Tlv>
        {
            new Tlv(0x5F29, Data.FromHex("00")),
            new Tlv(0x42, Data.FromHex("54455354")),
            new Tlv(0x7f49, new List<Tlv>
            {
                new Tlv(0x06, Data.FromHex("04007F00070202020203")),
                new Tlv(0x81, Data.FromHex("00")),
                new Tlv(0x82, Data.FromHex("00")),
                new Tlv(0x83, Data.FromHex("00")),
                new Tlv(0x84, Data.FromHex("00")),
                new Tlv(0x85, Data.FromHex("00")),
                new Tlv(0x86, Data.FromHex("00")),
                new Tlv(0x87, Data.FromHex("01"))
            }),
            new Tlv(0x5f20, Data.FromHex("54455354")),
            new Tlv(0x7f4c, new List<Tlv>
            {
                new Tlv(0x06, Data.FromHex("04007F000703010201")),
                new Tlv(0x35, Data.FromHex("00"))
            }),
            new Tlv(0x5f25, Data.FromHex("00")),
            new Tlv(0x5f24, Data.FromHex("00")),
        }),
        new Tlv(0x5f37, Data.FromHex("00"))
    });
    Debug.WriteLine(x.HexValue);

           
I won't include code for the Tlv class because I hope it is pretty self-explanatory. The output of my code is:
    7F4E4E5F2901004204544553547F4921060A04007F000702020202038101008201008301008401008501008601008701015F2004544553547F4C0E060904007F0007030102013501005F2501005F2401005F370100

Formatted:
    7F4E 4E (78)
        5F29 01 (1): 00
        42 04 (4): 54455354
        7F49 21 (33)
            06 0A (10): 04007F00070202020203
            81 01 (1): 00
            82 01 (1): 00
            83 01 (1): 00
            84 01 (1): 00
            85 01 (1): 00
            86 01 (1): 00
            87 01 (1): 01
        5F20 04 (4): 54455354
        7F4C 0E (14)
            06 09 (9): 04007F000703010201
            35 01 (1): 00
        5F25 01 (1): 00
        5F24 01 (1): 00


How would I write this more idiomatically with Bouncy Castle in C#? For simple types, like Tag=06 I know I can use DerObjectIndentifier, but what about custom tags, and nesting?


Reply | Threaded
Open this post in threaded view
|

Re: Beginner question - how to create custom ASN.1 structure in C#

David Hook-3

There's a lot of application specific tags in this - you'd need to use the DerApplicationSpecific (I think). A lot of the other fields appear to be implicitly tagged - in  that case the tagged object and then whatever it is that's getting tagged. With implicit tagging it's difficult to tell exactly what you're looking at if you don't have the original definition.

Regards,

David

On 3/10/20 4:56 am, ziggi slaw wrote:
Beginner question - how to create custom ASN.1 structure in C#

I have to create a custom ASN.1 structure, currently I use my own Tlv object, but would like to know how to do it with Bouncy Castle.

An example:
    var x = new Tlv(0x7f21, new List<Tlv>
    {
        new Tlv(0x7f4e, new List<Tlv>
        {
            new Tlv(0x5F29, Data.FromHex("00")),
            new Tlv(0x42, Data.FromHex("54455354")),
            new Tlv(0x7f49, new List<Tlv>
            {
                new Tlv(0x06, Data.FromHex("04007F00070202020203")),
                new Tlv(0x81, Data.FromHex("00")),
                new Tlv(0x82, Data.FromHex("00")),
                new Tlv(0x83, Data.FromHex("00")),
                new Tlv(0x84, Data.FromHex("00")),
                new Tlv(0x85, Data.FromHex("00")),
                new Tlv(0x86, Data.FromHex("00")),
                new Tlv(0x87, Data.FromHex("01"))
            }),
            new Tlv(0x5f20, Data.FromHex("54455354")),
            new Tlv(0x7f4c, new List<Tlv>
            {
                new Tlv(0x06, Data.FromHex("04007F000703010201")),
                new Tlv(0x35, Data.FromHex("00"))
            }),
            new Tlv(0x5f25, Data.FromHex("00")),
            new Tlv(0x5f24, Data.FromHex("00")),
        }),
        new Tlv(0x5f37, Data.FromHex("00"))
    });
    Debug.WriteLine(x.HexValue);

           
I won't include code for the Tlv class because I hope it is pretty self-explanatory. The output of my code is:
    7F4E4E5F2901004204544553547F4921060A04007F000702020202038101008201008301008401008501008601008701015F2004544553547F4C0E060904007F0007030102013501005F2501005F2401005F370100

Formatted:
    7F4E 4E (78)
        5F29 01 (1): 00
        42 04 (4): 54455354
        7F49 21 (33)
            06 0A (10): 04007F00070202020203
            81 01 (1): 00
            82 01 (1): 00
            83 01 (1): 00
            84 01 (1): 00
            85 01 (1): 00
            86 01 (1): 00
            87 01 (1): 01
        5F20 04 (4): 54455354
        7F4C 0E (14)
            06 09 (9): 04007F000703010201
            35 01 (1): 00
        5F25 01 (1): 00
        5F24 01 (1): 00


How would I write this more idiomatically with Bouncy Castle in C#? For simple types, like Tag=06 I know I can use DerObjectIndentifier, but what about custom tags, and nesting?



Reply | Threaded
Open this post in threaded view
|

Re: Beginner question - how to create custom ASN.1 structure in C#

ziggi slaw
Hey David,

had to go back to the ASN.1 basics and learn about tagging (implicit and explicit) and some other stuff...

I solved my problem with different BC classes, for example:
    var certificateProfileIdentifier = new DerApplicationSpecific(0x29, Hex.Decode("00")); // Output tag 5F29
    var certificationAuthorityReference = new DerApplicationSpecific(0x02, Strings.ToByteArray("ZZCVCA00001")); // Output tag 42
    var access = new DerApplicationSpecific(0x13, new byte[] { 0xc1 }) // Output tag 53
    var primeModulus = new DerTaggedObject(false, 0x01, new DerOctetString(curve.Curve.Field.Characteristic.ToByteArrayUnsigned())); // Output tag 82

Thanks!


On Tue, Oct 6, 2020 at 3:40 AM David Hook <[hidden email]> wrote:

There's a lot of application specific tags in this - you'd need to use the DerApplicationSpecific (I think). A lot of the other fields appear to be implicitly tagged - in  that case the tagged object and then whatever it is that's getting tagged. With implicit tagging it's difficult to tell exactly what you're looking at if you don't have the original definition.

Regards,

David

On 3/10/20 4:56 am, ziggi slaw wrote:
Beginner question - how to create custom ASN.1 structure in C#

I have to create a custom ASN.1 structure, currently I use my own Tlv object, but would like to know how to do it with Bouncy Castle.

An example:
    var x = new Tlv(0x7f21, new List<Tlv>
    {
        new Tlv(0x7f4e, new List<Tlv>
        {
            new Tlv(0x5F29, Data.FromHex("00")),
            new Tlv(0x42, Data.FromHex("54455354")),
            new Tlv(0x7f49, new List<Tlv>
            {
                new Tlv(0x06, Data.FromHex("04007F00070202020203")),
                new Tlv(0x81, Data.FromHex("00")),
                new Tlv(0x82, Data.FromHex("00")),
                new Tlv(0x83, Data.FromHex("00")),
                new Tlv(0x84, Data.FromHex("00")),
                new Tlv(0x85, Data.FromHex("00")),
                new Tlv(0x86, Data.FromHex("00")),
                new Tlv(0x87, Data.FromHex("01"))
            }),
            new Tlv(0x5f20, Data.FromHex("54455354")),
            new Tlv(0x7f4c, new List<Tlv>
            {
                new Tlv(0x06, Data.FromHex("04007F000703010201")),
                new Tlv(0x35, Data.FromHex("00"))
            }),
            new Tlv(0x5f25, Data.FromHex("00")),
            new Tlv(0x5f24, Data.FromHex("00")),
        }),
        new Tlv(0x5f37, Data.FromHex("00"))
    });
    Debug.WriteLine(x.HexValue);

           
I won't include code for the Tlv class because I hope it is pretty self-explanatory. The output of my code is:
    7F4E4E5F2901004204544553547F4921060A04007F000702020202038101008201008301008401008501008601008701015F2004544553547F4C0E060904007F0007030102013501005F2501005F2401005F370100

Formatted:
    7F4E 4E (78)
        5F29 01 (1): 00
        42 04 (4): 54455354
        7F49 21 (33)
            06 0A (10): 04007F00070202020203
            81 01 (1): 00
            82 01 (1): 00
            83 01 (1): 00
            84 01 (1): 00
            85 01 (1): 00
            86 01 (1): 00
            87 01 (1): 01
        5F20 04 (4): 54455354
        7F4C 0E (14)
            06 09 (9): 04007F000703010201
            35 01 (1): 00
        5F25 01 (1): 00
        5F24 01 (1): 00


How would I write this more idiomatically with Bouncy Castle in C#? For simple types, like Tag=06 I know I can use DerObjectIndentifier, but what about custom tags, and nesting?