Score:1

why openssl rsa shows a different content of a certificate?

fi flag

If I run cat my.key, I get:

-----BEGIN RSA PRIVATE KEY-----
MIIJKQIBAAKCAgEAu+UkIRXNZrdcnuCLsBsz/HiBcYNoAAAhYi2hISKBxkqX165U...

but if I run openssl rsa -in my.key, I get a different content as:

-----BEGIN PRIVATE KEY-----
MIIJQwIBADANBgkqhkiG9w0BAQEFAASCCS0wggkpAgEAAoICAQC75SQhFc1mt1ye...

why the contents are different, shouldn't they be the same?

dave_thompson_085 avatar
cn flag
Compare (cross-stack) https://superuser.com/questions/606215/openssl-pkcs8-default-format-gives-rsa-private-key and https://stackoverflow.com/questions/48958304/pkcs1-and-pkcs8-format-for-rsa-private-key but beware those are from before 2021 when OpenSSL 3.0.0 changed the defaults as I note on fgrieu's answer. Also note this is a key (in two formats) NOT a certificate.
Score:2
ng flag

The two base64 fragments in the question encode bytes which I show below in hex with spaces inserted to break things per ASN.1, and (for the first fragment corresponding to my.key) restore alignment with the second fragment.

                                                           30820929 020100 02820201 00BBE5242115CD66B75C9EE08BB01B33FC7881718368000021622DA1212281C64A97D7AE5454
30820943 020100 300D 0609 2A864886F70D010101 0500 0482092D 30820929 020100 02820201 00BBE5242115CD66B75C9E

We see that openssl rsa -in my.key has inserted a header of 26 bytes. As pointed by dave_thompson_085 in comment, this header is for a PrivateKeyInfo as defined in PKCS#8/RFC 5208. It's ASN.1 on the tune of:

SEQUENCE (0x0943=2371 bytes, 3 elements)
  INTEGER 0 // version
  SEQUENCE (0x0D=13 bytes, 2 elements) // privateKeyAlgorithm
    OBJECT IDENTIFIER (0x09=9 bytes) 1.2.840.113549.1.1.1 // rsaEncryption (PKCS #1)
    NULL // parameters
  OCTET STRING (0x092D=2349 bytes) // privateKey (header)

The big picture is that this PKCS#8 header tells what follows, so that programs can consistently recognize that the private key is for RSA rather than another algorithm.

In this

  • version is 0 (the first and AFAIK only PKCS#8 PrivateKeyInfo format)
  • the privateKeyAlgorithm is a PrivateKeyAlgorithmIdentifier, which itself is an AlgorithmIdentifier per PKCS#1v2.2 appendix C p.53, that is:
  • the privateKey in an OCTET STRING which bytes are the key itself, here an RSAPrivateKey SEQUENCE per PKCS#1v2.2 appendix C p.56, with 9 elements. This wrapping allows PKCS#8 to wrap keys uniformly, regardless of if they are defined as a SEQUENCE, another ASN.1 type, or structured per some other convention.
  • the optional attributes that could follow the key bytes is not present.

OpenSSL 3 by default uses PKCS#8 format to output keys, e.g.

openssl genrsa 4096
openssl req -nodes -newkey rsa:4096

Some OpenSSL tools have a flag to output the old format without PKCS#8 header, e.g.

openssl genrsa -traditional 4096

Note: the question disclosed the first bytes of the public modulus, but nothing confidential about the private key.

dave_thompson_085 avatar
cn flag
The difference is the RSA-specific structure defined by PKCS1 e.g. [rfc8017](https://www.rfc-editor.org/rfc/rfc8017.html#appendix-A.1.2) versus a PKCS8 structure defined in [rfc5208](https://www.rfc-editor.org/rfc/rfc5208.html#section-5). To avoid confusion, note `openssl genrsa` and `openssl rsa` (both) output PKCS1 in older OpenSSL (through 1.1.1), and many existing Stack posts use and depend on this, but they default to PKCS8 and require `-traditional` for PKCS1 in OpenSSL 3.0.0 up. OTOH `req -newkey` or `genpkey` or `pkey` have output PKCS8 since 1.0.0.
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.