ASN.1 Octet Strings

Cocoanetics picture Cocoanetics · Mar 8, 2013 · Viewed 7.1k times · Source

I'm decoding a X.509 Certificate in ASN.1 format. I'm decoding it successfully, traversing the structure, but there is one thing that I don't understand.

There are some scenarios where I get an octet string and this website that I am playing with (http://lapo.it/asn1js/) shows that these octet strings actually contain more of the ASN.1 tree. This website annotates such octet strings with (encapsulates)

My question is this: how do I know during parsing that an octet string actually encapsulates something more? Do I just try to parse it, looking if I get a tag and valid length? If not then it is pure bytes data? And if yes then it is a valid sub-tree?

Or is this meant to be output as bytes and the consumer should then only try to parse it if he knows that it is encoded data from for certain keys?

Take the example that is already loaded on the site and hit "decode". I am referring for example to offset 332 which is an octet string that encapsulates a bit string.

Answer

mr.spuratic picture mr.spuratic · Mar 9, 2013

This is what "extensions" looks like in ASN.1 speak (RFC 2459 §B.2 — I know that RFC is "obsolete", but that useful appendix isn't present in the later versions).

Extensions ::= SEQUENCE OF Extension

Extension ::= SEQUENCE {
extnId     OBJECT IDENTIFIER,
critical   BOOLEAN DEFAULT FALSE,
extnValue  OCTET STRING }

Every extension payload is encapsulated within an OCTET STRING. The OID of the extensions tells you what to expect within that octet string. In the case of keyUsage it's a BIT STRING (§4.2.1.3).

And now I have an answer about my own question on subjectAltName, it's in §4.2.1.7.

One benefit of using OCTET STRING for the content is that, as per spec, unknown (non-critical) extensions can be identified as such and trivially be skipped over (though I think DER makes it trivial too).