Converting keys between openssl and openssh
If I use the following
openssl req -x509 -days 365 -newkey rsa:2048 -keyout private.pem -out public.pem -nodes
If I use
ssh-keygen -t rsa -f rsa
Is it possible to convert from the format of
Edit: To be more specific,
a) If I have the
public.pemgenerated by the above command, how do I get the equivalent rsa private key and public key?
b) Given the
rsa.pub, how do I get the x509 keys if I do know the additional metadata that the above openssl command takes in?
If I go from the openssh format to x509 and back, I should ideally get the same key file back.
Please do not reuse the same private key for different purposes/protocols like ssl+ssh. Crypto audits of any protocol always assume the private key is not used anywhere else. Bugs like 3-Shake show that it is hard to design crypto securely just within the SSL/TLS protocol.
You are missing a bit here.
ssh-keygencan be used to convert public keys from SSH formats in to PEM formats suitable for OpenSSL. Private keys are normally already stored in a PEM format suitable for both.
However, the OpenSSL command you show generates a self-signed certificate. This certificate is not something OpenSSH traditionally uses for anything - and it definitely is not the same thing as a public key only.
OpenSSH does have support for certificates as well, but it is likely that you are not using this support. Also, these certificates are not X.509, so they are incompatible with OpenSSL.
The certificate contains information that is not present anywhere else and each certificate is unique and can not be recreated at will. This means that you need to store the X.509 certificate, in addition to the private key, if you wish use the same key for both OpenSSL and OpenSSH.
If you just want to share the private key, the OpenSSL key generated by your example command is stored in
private.pem, and it should already be in PEM format compatible with (recent) OpenSSH. To extract an OpenSSH compatible public key from it, you can just run:
ssh-keygen -f private.pem -y > private.pub
If you want to start from OpenSSH and work your way over to the OpenSSL side, with a self-signed certificate (for whatever reason), here's how:
$ ssh-keygen -f test-user Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in test-user. Your public key has been saved in test-user.pub. The key fingerprint is: ff:36:f1:74:c7:0d:4e:da:79:5c:96:27:2c:2c:4e:b6 [email protected] The key's randomart image is: +--[ RSA 2048]----+ | | | | | . . .| | + o =.+| | S+ o * B+| | .E o = B| | . + o.| | .o . | | ... | +-----------------+ $ openssl req -x509 -days 365 -new -key test-user -out test-user-cert.pem You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]: State or Province Name (full name) [Some-State]: Locality Name (eg, city) : Organization Name (eg, company) [Internet Widgits Pty Ltd]: Organizational Unit Name (eg, section) : Common Name (e.g. server FQDN or YOUR name) : Email Address : $ ls -l test-user* -rw------- 1 naked naked 1675 Mar 18 21:52 test-user -rw-r--r-- 1 naked naked 1229 Mar 18 21:53 test-user-cert.pem -rw-r--r-- 1 naked naked 392 Mar 18 21:52 test-user.pub
From these, both
test-user-cert.pemfiles are critical to preserve, where as
test-user.pubcan always be recreated from test-user as needed.
Thanks! That helped clarify things quite a bit. Can I use the ssh keys to self sign and generate this cert?
You can, the private key is already in a compatible format so you can just use that in the OpenSSL command.
Could you please explain how would I use that private key in the openssl command? I tried reading the man but didn't get anywhere.
I did, although I am beginning to doubt this will actually be useful for you - if you are still wishing for ideal back-and-forth conversion without losing data, that is just not a meaningful goal.