Score:1

How to recognize DER nested OCTET SEQUENCEs?

in flag

I asked OpenSSL to generate a dummy Ed25519 private key for me and got this output:

https://lapo.it/asn1js/#MC4CAQAwBQYDK2VwBCIEIJCO9eKZEUOmL9CGfecuKqvYU_hLTAFXwl0Ipd8xNXbP

It decodes to the following:

SEQUENCE (3 elem)
  INTEGER 0
  SEQUENCE (1 elem)
    OBJECT IDENTIFIER 1.3.101.112 curveEd25519 (EdDSA 25519 signature algorithm)
  OCTET STRING (34 byte) 0420908EF5E2991143A62FD0867DE72E2AABD853F84B4C0157C25D08A5DF313576CF
    OCTET STRING (32 byte) 908EF5E2991143A62FD0867DE72E2AABD853F84B4C0157C25D08A5DF313576CF

How does a DER parser know when an OCTET STRING contains DER within it? The first OCTET STRING starts with 04 20 indicating a 32-byte OCTET STRING, and the "parent" starts with 04 22 specifying an OCTET STRING that includes the "child" header 04 20. There doesn't seem to be a flag saying that "there is a DER-encoded subobject inside here".

OpenSSL dumps it like this:

myria@MYRIA:~$ openssl asn1parse -i -in dummy-private.pem
    0:d=0  hl=2 l=  46 cons: SEQUENCE
    2:d=1  hl=2 l=   1 prim:  INTEGER           :00
    5:d=1  hl=2 l=   5 cons:  SEQUENCE
    7:d=2  hl=2 l=   3 prim:   OBJECT            :ED25519
   12:d=1  hl=2 l=  34 prim:  OCTET STRING      [HEX DUMP]:0420908EF5E2991143A62FD0867DE72E2AABD853F84B4C0157C25D08A5DF313576CF

indicating that OpenSSL just sees the "parent" OCTET STRING.

dave_thompson_085 avatar
cn flag
There is no flag, and no fixed rule; it depends on the information in the containing structure, which here (like most but not all privatekeys written by OpenSSL since 1.0.0 in 2010) is PKCS8. In particular PKCS8 for RSA contains an ASN.1 SEQUENCE defined in PKCS1 (rfc2437/3447/4347/8017), for DSA contains an ASN.1 SEQUENCE apparently made up by Eric, for ECDSA/ECDH contains an ASN.1 SEQUENCE defined by SEC1 (rfc5915), and for EdDSA/XDH AFAIK there is no standard but OpenSSL as you observed does an OCTET STRING.
dave_thompson_085 avatar
cn flag
Note if _you_ know there is an embedded encoding, you can tell `openssl asn1parse` to decode/display it with `-strparse`; see the man page. In contrast lapo.it will always try to decode a BITSTRING or OCTETSTRING if it can, which may result in garbage decodes of things that are in fact NOT embedded ASN.1. Although this data is related to crypto, this isn't really a cryptography question or issue.
Myria avatar
in flag
@dave_thompson_085 I wish there were a way to promote comments to answers! Thanks!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.