Score:2

How to find the public key in a digital certificate

cn flag

I can see what I think should be the public key in the Details tab for StackOverflow's certificate (see screenshot below). However, it is 2160 (num of chars (540) multiplied by 4 = 2160) bits long, not 2048 as it specifies.

When I view the certificate details using openssl in cmd, I can see a smaller version of the public key which is 514 nibbles * 4 = 2056 bits, which is one byte off 2048.

I noticed the first byte is 00. Is the first byte to be ignored because it's 0? Why is it displayed in openssl?

If the first byte is to be ignored, does this mean the actual public key starts with b2 and ends in af?

Why does Windows show extra bytes before and after the public key in the Details tab?

I've used Notepad in the screenshot to show the full value of what's in the Details tab.

enter image description here

Update:

I realised the majority of the bytes is the modulus, and the exponent 0x010001 is at the end:

enter image description here

But what is the 02 03 between the modulus and the exponent?

And what is the 30 82 01 0a 02 82 01 01 00 before the modulus?

fgrieu avatar
ng flag
Try pasting the hex in an [ASN.1 decoder](https://lapo.it/asn1js/)
dave_thompson_085 avatar
cn flag
Your question is not finding the publickey -- you already found it. Your question is _decoding_ it. As https://datatracker.ietf.org/doc/html/rfc3279#section-2.3.1 says it is the DER encoding of ASN.1 structure RSAPublicKey from PKCS1, and as meshcollider says DER is now in X.690, although https://en.wikipedia.org/wiki/X.690 is easier to read than the actual ITU-T document, and so is https://letsencrypt.org/docs/a-warm-welcome-to-asn1-and-der/ .
Score:4
gb flag

As per X.690, the format begins with 0x30 to denote a sequence. Next comes the total length. The first byte of the length is 0x82. Because the highest bit is set here, it means this byte encodes the length of the length. In other words, the next 2 bytes encode the actual length. Those bytes are 0x010a, or 266 bytes.

Now we hit another tag. This time it is 0x02, meaning an integer. The length starts with 0x82 again, so we know the subsequent 2 bytes encode the length. These are 0x0101, so the total length of this integer is 257 bytes. These 257 bytes are the ones you highlighted. The reason the extra 0x00 byte is included in the start is because BER integers are signed and represented in 2's complement - if there was no leading null byte, it would be interpreted as a negative number, since the next byte 0xb2 has the highest bit set.

After those 257 bytes for the modulus, we hit another tag: 0x02 again. Another integer. This time the first byte (0x03) of the length doesn't have the highest bit set, so the length is simply 3 bytes. These 3 bytes are 0x010001, or 65537. That's the public exponent.

And that totals 266 bytes, as expected for the whole sequence. It is just those 2 integers.

DannyNiu avatar
vu flag
I edited the answer to explain why the leading nul byte is there.
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.