6
$\begingroup$

May sound stupid to many, but I would like to have some pointers on what exactly is contained inside a private key. I have decent understanding of public/private keys/certificates (have created them many times) and their purpose but would like to take a step back and see what is inside them using a "dump" utility or something - Would I be able to see big prime numbers etc contained inside the private key?

Is there a utility to show me the "logical content" of the output of the following command as opposed to a bunch of ASCII characters?

openssl genrsa

generates

-----BEGIN RSA PRIVATE KEY-----
MIIBPAIBAAJBANdINmZY7VuoRy5VZYwnVIAE/0sd/HkaDVXfMNpwVeKo7K2XLC6U
jpnPFP2MaDEqxs0T6cKVMmt5FsNGyfdKbvcCAwEAAQJBAMGuPQrtPHY2uftsZtSl
2bbnSAr7qlYFYzP7fYc4g3xLWSc5viAe7KgsU7+jXNCqEvflf4iOxhCadUK07APG
…
-----END RSA PRIVATE KEY-----

Any pointers appreciated. Same question also applies to the public key btw..

$\endgroup$
  • $\begingroup$ Try the pkey option. $\endgroup$ – rath Dec 16 '13 at 23:16
  • $\begingroup$ thx rath for the pointer. The following command worked for me. . . openssl rsa -in privkey.pem -text $\endgroup$ – user1813603 Dec 16 '13 at 23:37
  • 4
    $\begingroup$ Good to know. May I suggest that you write an answer showing how the RSA key components get displayed? $\endgroup$ – rath Dec 16 '13 at 23:59
  • 3
    $\begingroup$ This question appears to be off-topic because it is about using openssl and the format it uses, rather than about the cryptographic keys themselves. It would be on-topic on Unix & Linux. $\endgroup$ – Gilles Dec 19 '13 at 19:24
  • 4
    $\begingroup$ Possible duplicate of What data is saved in RSA private key? $\endgroup$ – Vadzim Feb 6 '18 at 16:45
7
$\begingroup$
$ openssl genrsa | openssl rsa -text -noout
Private-Key: (512 bit)
modulus:
    00:e7:be:c0:b7:7a:8a:e6:58:c3:dc:3e:eb:ed:bc:
    a7:15:04:78:8d:9d:fe:a2:83:aa:ca:85:5f:4b:ae:
    5c:fa:3d:bd:2b:a9:91:58:e1:da:d8:8a:bd:25:6d:
    07:10:74:52:2f:ee:ce:bd:3c:c6:89:01:2e:ff:9a:
    3b:61:4d:e7:81
publicExponent: 65537 (0x10001)
privateExponent:
    00:8d:b9:23:44:51:e5:c6:0e:fc:e0:a1:7e:49:2a:
    79:07:aa:6f:4b:34:17:38:2d:cb:72:04:f4:8d:64:
    f9:a9:72:94:30:6e:d8:65:81:e7:be:05:a8:19:fb:
    82:c9:77:b2:fa:76:0d:4b:ff:b3:ad:a9:f1:9e:55:
    cd:b3:d2:c8:41
prime1:
    00:fc:ea:3f:dd:a9:5f:6f:4d:05:41:50:04:81:8e:
    c7:6b:a0:95:d3:d4:36:09:73:b4:b8:06:db:fc:f2:
    89:0c:e9
prime2:
    00:ea:92:65:f9:06:58:11:f4:bc:fe:e6:10:0b:80:
    51:73:18:1b:91:24:27:83:ab:c9:b3:4c:79:01:1f:
    60:86:d9
exponent1:
    00:e6:7b:63:30:51:c5:d2:dc:51:c9:af:6e:2b:d3:
    3e:10:eb:0b:1f:3b:e8:f2:bc:2b:18:f9:c7:48:c0:
    8d:fc:e1
exponent2:
    11:b3:04:30:bb:12:d0:20:08:56:af:63:4c:8a:dd:
    1a:73:1a:39:64:61:fa:e4:6e:6e:b1:f9:7b:65:33:
    b2:59
coefficient:
    3a:6d:f6:8f:4b:d2:c3:a8:53:aa:32:0d:b9:c5:50:
    d8:db:9c:e3:9b:a8:40:c3:c0:14:2b:7e:67:25:67:
    b7:03

The numbers are in hexadecimal with a funny format, except for the public exponents. You can convert them to your favorite format with a bit of text processing.

Python is a nicer environment for playing with common cryptographic primitives such as RSA. Install pycrypto in addition to the core Python distribution.

~% python                    
Python 2.7.3 (default, Apr 10 2013, 06:20:15) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Crypto.PublicKey import RSA
>>> k = RSA.generate(1024)
>>> k.n
137989966843141497713268840304515414544555471898207567571275317377632553064486587462119814017348007187827660662823764767983835450392238729966453378972206066751517751868783987379434607487796692691455662440665457077710749398149038850219502976135918708465391309679715881739357423413344802810741483299360557935787L
>>> k.p * k.q
137989966843141497713268840304515414544555471898207567571275317377632553064486587462119814017348007187827660662823764767983835450392238729966453378972206066751517751868783987379434607487796692691455662440665457077710749398149038850219502976135918708465391309679715881739357423413344802810741483299360557935787L
>>> (k.d * k.e) % ((k.p - 1) * (k.q - 1))
1L
$\endgroup$
  • $\begingroup$ How many digits can n have for 1024-bit key? $\endgroup$ – user1511417 Dec 11 '17 at 23:45
  • 1
    $\begingroup$ @user By definition, in a 1024-bit key, $n$ has 1024 digits in base 2. $\endgroup$ – Gilles Dec 12 '17 at 6:00
1
$\begingroup$

Firstly, there are two common formats to store such values: PEM and DER. PEM is what you posted. It is, actually, the same data as DER, but base-64 encoded.

Secondly, There is a thing which is called 'ASN.1' structure. Basically, an ASN.1 structure is a set of fields of some basic types, such as INTEGER, BOOLEAN, SEQUENCE and others.

In the previous post, you can see one concrete example of this ASN.1 structure.

On different platforms there exist tools which are able to read these ASN.1 structures, as well as write them. There is even a tool, you feed it the specification of your ASN.1 structure in some format, and it generates a C code which can read files of this structure and write them.

For things such as, let's say, RSA private key, this ASN.1 structure is defined in the standard:

PKCS#1 (RFC 3447) defines the ASN.1 structure for RSA Private key. It must be used by everyone. Of course, you can create your own:) but I think you realise why standardisation is important.

You can play with it that way: generate your own RSA key pair (create some short one). Then take this .PEM file and copy this base-64 encoded string to ASN.1 decoder (there are a couple of online decoders, such as https://lapo.it/asn1js/). And take a look! :) You will see a long line of numbers, and you can make sense of them in some text editor.

As far as I remember, first several numbers are a header, and then it goes that way: (TYPE)(LENGTH)(DATA)(TYPE)(LENGTH)(DATA)(TYPE)(LENGTH)(DATA)(TYPE)(LENGTH)(DATA)

for instance, 020108

02 - means 'int' 01 - means 'of length 1' 08 - is your int

and the ASN.1 parser will read all values that way - read type, read length, then read (length) number of bytes, read type, read length, read (length) number of bytes .......

After that, since the parser knows that it was a ASN.1 structure for RSA private key, he will say 'ok, that field was the first prime and that field was the second prime'

$\endgroup$
  • $\begingroup$ There's a lot more than two. OpenSSL genrsa uses PKCS1, either DER or PEM and optionally encrypting the PEM; other OpenSSL functions use PKCS8 which embeds PKCS1 for the actual key but encrypts very differently, and PKCS12 which embeds PKCS8 with more differences in encryption. OpenSSH sometimes uses OpenSSL formats but sometimes very different ones; other SSH implementations use other different formats, as do PGP (and GPG), and Microsoft, and PKCS11, and other things. Some of these are ASN.1 and some aren't. $\endgroup$ – dave_thompson_085 Jul 7 '17 at 4:45

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.