1

Background

With openssl version 1.1.1n, I used to use

openssl genrsa -out myFile.pem

and my key file would look like this:

-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----

Problem

However, I recently updated to Ubuntu 22.04 and now I have openssl version 3.0.2. Now when I run that command, the output file looks like this:

-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----

Notice that the "RSA" is missing. My application now says "unsupported key format" because of this.

What I Tried

I tried using

openssl genpkey -algorithm RSA -out myFile.pem

because the docs for genrsa say it's deprecated and recommend using genpkey instead. But this gave the same results (the file is missing the "RSA" part).

Question

How can I properly generate an RSA key using openssl that matches the old format?

nullromo
  • 2,165
  • 2
  • 18
  • 39
  • Does this answer your question? [How to convert a private key to an RSA private key?](https://stackoverflow.com/questions/17733536/how-to-convert-a-private-key-to-an-rsa-private-key) – Ouroborus Mar 07 '23 at 22:29
  • (0) this is not programming or development and (1) an application that doesn't support PKCS8 format (the default in OpenSSL 3.0 up) is way out of date and probably insecure, but (2) in OpenSSL 3.0 up you can still get the obsolete PKCS1 format with **`genrsa -traditional`** (as stated on the man page you link) or by filtering the result through either `rsa -traditional` or `pkey -traditional` – dave_thompson_085 Mar 07 '23 at 22:35
  • Thank you for pointing out the PKCS thing. I had head of PKCS before, but I didn't know the relationship between PEM, RSA, and PKCS. For anyone else curious about this, [here](https://stackoverflow.com/a/48960291/4476484) is an explanation. – nullromo Mar 07 '23 at 22:45
  • As for the `-traditional` flag, I had no idea what that meant since I didn't know what PKCS#1 and PKCS#8 meant. Now I understand that the file with the "`RSA`" in it is in PKCS1 format whereas the file without the "`RSA`" in it is in PKCS8 format, even though both are still PEM files. – nullromo Mar 07 '23 at 22:46

1 Answers1

1

Solution

You can use

openssl genrsa -out myFile.pem -traditional

to get the right file format.

Alternative

For a more backwards compatible command, you can use ssh-keygen instead:

ssh-keygen -t rsa -m PEM -f myFile.pem -N "" <<< y

The <<< y part will answer the "do you want to overwrite the existing file" interactive prompt.

Side Note

The normal output of ssh-keygen (which is an "OpenSSH private key") is actually supported by my application, so the -m PEM was not necessary in my case.

nullromo
  • 2,165
  • 2
  • 18
  • 39