I have the following bytes array storing a certificate chain (DER encoded):


What is the public key and is the offset to read it constant?

  • 1
    Have you used a DER decoder? There are online tools for this. – schroeder Mar 31 '17 at 15:16
  • Can you link a working tool? I tried lapo.it/asn1js but don't know what exactly is the public key? Later I want to read it out, using my own code. But having a working online tool would already help much, thanks. – floyd Apr 1 '17 at 19:33
  • Note: that encoding is a certificate, not a chain. If you did have a chain, you would need the position of the desired cert in the chain, which might or might not vary, as well as the position of the key in the desired cert, which varies. – dave_thompson_085 Aug 30 '17 at 5:09

If you send the blob to lapo.it's decoder the SubjectPublicKeyInfo starts at offset 160.

Depending on what you mean by "the public key" it either starts at 160, 183, or 186. (SPKI, SPKI.PublicKey, or the content bytes of the bit string). You really need the whole SPKI, since the key id says how to read the payload, and for ECC the parameters section identified the curve.

The offset is definitely not fixed for an arbitrary certificate, the subject name, serial number, and issuer name all precede it having variable length depending on their content. You'll definitely need to use or write a DER parser to find it.


We can search for specific fields using https://wiki.openssl.org/index.php/Manual:Asn1parse(1)

All we need to do is run this openssl command:
openssl asn1parse -inform DER -in <your-cert>.der

Depending on the length of certain fields like Subject Name, the location of field you were interested in varies. Here is an example output from the above command for X509v3 Subject Key Identifier field:

547:d=5  hl=2 l=   3 prim: OBJECT            :X509v3 Subject Key Identifier
552:d=5  hl=2 l=  22 prim: OCTET STRING      [HEX DUMP]:<Truncated>

We can notice the existence of the field at offset 547.

  • That's the key identifier (often only one of several identifiers, but the one given the honor of being an extension in the cert). It is NOT the key. As bartonjs correctly said months ago, the key is in the field SubjectPublicKeyInfo (and asn1parse can't label fields). – dave_thompson_085 Aug 30 '17 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.