1

I know this cannot be as hard as I am making it.. I've built a structure to encode things into Asn1 (using BouncyCastle, C#), no problem.

Trying to extract an integer back out is easy enough (BouncyCastle: (DerInteger) object.Value.IntValue ), but DerOctetStrings doesn't seem to have direct functionality - likely because it can represent ascii strings AND byte[].

So {04 02 46 45} represents both the byte[] { 0x46, 0x45 } and the string "FE"

... and I can't figure out how to get either, short of manually stripping off the tag and using the length to grab the bytes that follow. All the BouncyCastle methods seem to shoot back out all the octets ( 04 02 46 45 ) or encode them again ( 04 06 04 02 46 45 )

I'd really like to stick to BouncyCastle just to keep it parallel to the encoder I wrote, and because it has built-in checking of the length field that throws properly. .NET is fine if I have to go that route and it is easier. And worse case I'll do it all manually but.. that's ugly.

Main question: How do you use built-in functionality to extract the content bytes out of a DerOctetString (without the tag and length)? Secondary question: What is the easiest way to take those output bytes into a normal C# string?

-- Aside: I have only managed to find two posts similar to this on here: How do I decode a DER encoded string in Java? Which goes directly to the string version, can't just grab the bytes out

And BouncyCastle Java - Decoding DER encoded OCTET strings But that only talks about a java library so it doesn't help me

======================================================== Edit: Resolved the main portion of the question. It was the .GetOctets (so accepting that answer), the underlying issue was just an unclear constructor from BouncyCastle.

So to add to that, if anyone wants to feed Asn.1 into BouncyCastle don't use:

DerOctetString(byte[] asn1ByteArray);

use the inherited

Asn1Object.FromByteArray(byte[] asn1ByteArray);

If I figure out the string bit I'll edit again with that solution, though for now I'll just deal with the clearer UTF8 strings

Community
  • 1
  • 1
Daniel Cazan
  • 323
  • 1
  • 4
  • 13

1 Answers1

3

I just use the java version so I am not sure if this helps you in c# but have you tried to get the content of the DEROctetString with getOctets()?

It does exactly what you want.

MatK
  • 96
  • 2
  • I had and it wasn't working, but I finally figured out what the issue was (with part of it, I'm still figuring out the string bit) The issue was actually coming from the encoding. DerOctetString(byte[]) sounds like you feed it ASN.1 and it parses it out into a DerOctetString object, but in this instance it expects the bytes to be the content (without the tag). This is a tad different from how some other constructors treat the byte[]. – Daniel Cazan Apr 15 '14 at 20:44
  • I think in most BC classes the constructor just takes the "content" of the type and to parse an existing type you have to use a utility method or an `ASN1InputStream`. But maybe the interface is different in c#. Regarding your string problem, if the content really is a string in java at least you just can use `new String(dos.getOctets())`, but most of the time a `DEROctetString` just contains another ASN1 Object encoded as bit string. So you have to feed the bytes into an `ASN1InputStream`. Exactly as implemented in the question you linked to as method `toDERObject(byte[] data)` – MatK Apr 16 '14 at 08:59