17/03/2013

OpenSSH key, Amazon EC2 RSA, Android and dropbear (from Terminal IDE)

Here is the thing: I have quite complicated SSH key (uses AES-128-CBC) and while there are posts about converting the openssh key to dropbear format, they fail to mention that the openssh key needs to be in some PEM (or more streamline) format. That might seam easy, but when you try to convert using the common example, you might end up getting an error like this:
/usr/lib/dropbear/dropbearconvert openssh dropbear ~/.ssh/id_rsa /tmp/id_rsa.db
Error: Ciphers other than DES-EDE3-CBC not supported
Error reading key from '~/.ssh/id_rsa'
That happens especially if you have keys generate by the Amazon Web Services, or EC2. Those generally look like this:
head -3 ~/.ssh/id_rsa_amazon_ec2
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,1234567890ABCSFTDHIJKLMNOPQRSTUV

Now, that sucks! And google-ing didn't get me very far. So, if you have no idea what to make from that error, well, fear not, as there is a simple solution, coming from openssl. You will need to convert your key to PEM and then, use the dropbearconvert tool like this:
openssl rsa -in ~/.ssh/id_rsa -outform pem > ~/.ssh/id_rsa.pem
You will need to enter your pass phrase if using one:
Enter pass phrase for id_rsa:
writing RSA key

At this point, technically, that key is the same but you have removed the passphrase (you could do the same by using ssh-keygen -p -f ~/.ssh/id_rsa, but then, you will have to put it back).

And now, using the converter on the PEM:
/usr/lib/dropbear/dropbearconvert openssh dropbear ~/.ssh/id_rsa.pem ~/.ssh/id_rsa.db

And you will get your key:
Key is a RSA key
Wrote key to '~/.ssh/id_rsa.db'

Hope this helps someone like me that uses the Amazon generated SSH keys and needs them in the Android side (by the way, I needed it for use with Terminal IDE ssh, which I love)... NOTE: You should remove that file from your system as it is password-less. This could allow someone to use it without knowing your pass phrase!