3

I did encrypt my private key using openssl pkcs8 -topk8 -in id_rsa -out id_rsa_new -v2 des3 (http://martin.kleppmann.com/2013/05/24/improving-security-of-ssh-private-keys.html)

But now, ssh-agent won't work anymore. When I try to connect to a machine using ssh:

OpenSSH_6.1p1 Debian-4, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to 192.168.178.38 [192.168.178.38] port 22.
debug1: Connection established.
debug3: Incorrect RSA1 identifier
debug3: Could not load "/home/damon/.ssh/id_rsa" as a RSA1 public key
debug1: identity file /home/damon/.ssh/id_rsa type 1
debug1: Checking blacklist file /usr/share/ssh/blacklist.RSA-2048
debug1: Checking blacklist file /etc/ssh/blacklist.RSA-2048
debug1: identity file /home/damon/.ssh/id_rsa-cert type -1
debug1: identity file /home/damon/.ssh/id_dsa type -1
debug1: identity file /home/damon/.ssh/id_dsa-cert type -1
debug1: identity file /home/damon/.ssh/id_ecdsa type -1
debug1: identity file /home/damon/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.0p1 Debian-3ubuntu1
debug1: match: OpenSSH_6.0p1 Debian-3ubuntu1 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.1p1 Debian-4
debug2: fd 3 setting O_NONBLOCK
debug3: load_hostkeys: loading entries for host "192.168.178.38" from file "/home/damon/.ssh/known_hosts"
debug3: load_hostkeys: found key type ECDSA in file /home/damon/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys
debug3: order_hostkeyalgs: prefer hostkeyalgs: [email protected],[email protected],[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: kex_parse_kexinit: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: [email protected],[email protected],[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected],[email protected],[email protected],ssh-rsa,ssh-dss
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,[email protected],hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,[email protected],hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,[email protected],zlib
debug2: kex_parse_kexinit: none,[email protected],zlib
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: first_kex_follows 0 
debug2: kex_parse_kexinit: reserved 0 
debug2: kex_parse_kexinit: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,[email protected],hmac-sha2-256,hmac-sha2-256-96,hmac-sha2-512,hmac-sha2-512-96,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,[email protected],hmac-sha2-256,hmac-sha2-256-96,hmac-sha2-512,hmac-sha2-512-96,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,[email protected]
debug2: kex_parse_kexinit: none,[email protected]
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: 
debug2: kex_parse_kexinit: first_kex_follows 0 
debug2: kex_parse_kexinit: reserved 0 
debug2: mac_setup: found hmac-md5
debug1: kex: server->client aes128-ctr hmac-md5 none
debug2: mac_setup: found hmac-md5
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA d0:42:c3:a4:bb:2e:f0:cd:38:c1:32:d5:a5:ac:4c:d5
debug3: load_hostkeys: loading entries for host "192.168.178.38" from file "/home/damon/.ssh/known_hosts"
debug3: load_hostkeys: found key type ECDSA in file /home/damon/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys
debug1: Host '192.168.178.38' is known and matches the ECDSA host key.
debug1: Found key in /home/damon/.ssh/known_hosts:1
debug1: ssh_ecdsa_verify: signature correct
debug2: kex_derive_keys
debug2: set_newkeys: mode 1
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug2: set_newkeys: mode 0
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /home/damon/.ssh/id_rsa (0x7f15643f1e30)
debug2: key: /home/damon/.ssh/id_dsa ((nil))
debug2: key: /home/damon/.ssh/id_ecdsa ((nil))
debug1: Authentications that can continue: publickey
debug3: start over, passed a different list publickey
debug3: preferred gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/damon/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug2: input_userauth_pk_ok: fp ee:e0:8c:60:ee:9f:05:4c:cd:0e:45:85:e2:91:64:95
debug3: sign_and_send_pubkey: RSA ee:e0:8c:60:ee:9f:05:4c:cd:0e:45:85:e2:91:64:95
Agent admitted failure to sign using the key.
debug1: Trying private key: /home/damon/.ssh/id_dsa
debug3: no such identity: /home/damon/.ssh/id_dsa
debug1: Trying private key: /home/damon/.ssh/id_ecdsa
debug3: no such identity: /home/damon/.ssh/id_ecdsa
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey).

I ran this on a freshly installed Linux Mint 15 x64 VM and on several other Linux Mint installations, all fail to connect.

Here is an example of a pkcs8 encrypted private key (I generated only to share, don't worry):

damon@DamonVirtual ~/.ssh $ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/damon/.ssh/id_rsa): id_rsa_test
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in id_rsa_test.
Your public key has been saved in id_rsa_test.pub.
The key fingerprint is:
f6:58:aa:43:c4:08:51:54:34:e5:5a:76:ae:ed:bd:30 damon@DamonVirtual
The key's randomart image is:
+--[ RSA 2048]----+
|  .+oo+..        |
|  .    o         |
|   . o  + .      |
|    . o+ o       |
|     .. S o      |
|      .. B       |
|     .  + E      |
|      .. . +     |
damon@DamonVirtual ~/.ssh $ openssl pkcs8 -topk8 -in id_rsa_test -out id_rsa_test_enc -v2 des3
Enter Encryption Password:
Verifying - Enter Encryption Password:
damon@DamonVirtual ~/.ssh $ chmod id_rsa_test_enc 600
chmod: Ungültiger Modus: »id_rsa_test_enc“
„chmod --help“ liefert weitere Informationen.
damon@DamonVirtual ~/.ssh $ chmod 600 id_rsa_test_enc
damon@DamonVirtual ~/.ssh $ cat id_rsa_test_enc
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIsRD8OicdxA0CAggA
MBQGCCqGSIb3DQMHBAhpBxvnSmEyaQSCBMj1vvjrx3KiEa++l1xtDDimOPXDqrh6
AAQBZGBBgDNX7OqmqDK6gvbliZUsYEBdbxA8g53tQe168tmXw6RbpDUqwRT0N8MU
an5pBJkOd1nT9TGFivhvBVRLeoCko9jmrcia38/Cc4D088rirYbTOdi47qB88zin
WVxNVCXTAB2IXAvgSVuYlbzRku++OCZAY4YI+4Q8diY1pnY5rMVNc1eTknMp2Gyc
s+37nVpc2Xz3kI1T/2yiJEkOucyyNxbvKfDdmkhxYnflghjQAIFZXhALgGwTAeD+
SkuCpHCyQ6Rw+X/lSvsyj9RNpioF51iTMazy1NbDSyv8FaB+cF8QLC9wz9hHZ3yV
xiNVy38yVwMDr4OudXB+qjc8nDSjQdK5Ade8OWQZS7KXiT0K0wdGYn6NSGuoJTU3
UI9m+Glwv5JI5WE9nUm2GyZzUTujhH7lzJKw/20/IhosrS78/TVWUJ5MW5jNrDz+
raKG9jTLjEirhQFvCowh2oYJUxq0Fmh5d2Iskq19rVdL8iDMyZMJxfsiKTxd6TcK
uGtFkX+IS80Is6r2ZnDsQwlMiKw1pZ223JAQvLHX6baBvoDGr7vSeb4m91G4DYOK
gzdAICHuX0bIXbPhNxfkpstakdvrR2Fz1QNpInTO8K4fQ9dqnCyeiferbzpEkhVD
BVVNRcBVynGPWs6SwBTkRW7D5tWNdFA/ngVkqDDn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e3EEwBjKjXlipTMYgLl2gi5Lt63f3esHYT0IBwN4DPz9BdYkb3vqsPSdouV2N45/S7w6dwwx2BJ/yP35r2G91ckOiHQSioDWFDbWQMYNrHrbAAtk6QbZthwMyKFK59iGCRltAxyq2/EPU6dkoyq8AWDZao+i65AQjIfDQ5vqHhPPZo80j5q8XNWP6E8oVx+hwUiYZ/hEHLR5KWMZyG0MVzDZICs9qMruIDA9YASl9DTNc72+5L5b1Ct5rTYGeq+5bMue9w7GVCFu/6ahPUwyNI17SW8Fsu6hF/HxbgnR damon@DamonVirtual

Encryption pass for this one is 123456789. Can anybody spot mistakes I did while generating and encrypting the key?

(Once again, this is not my personal private key, I just generated this one for test purposes, this isn't in any of my authorized_keys files, the debug output came from usage with another key generated the same way)

Is it me failing, or ssh-agent? I don't get a password prompt, so ssh-agent doesn't seem to recognize the key is encrypted... But I can add it to the agent using ssh-add id_rsa, there I'm prompted for the password, there it works...

edit

I think I found out what happens: ssh-agent tries to automatically add ssh-keys to it. When doing so, it horribly fails. This failure is indicated by unremovable identities. Try ssh-add -D && ssh-add -l. If this returns any remaining identities, ssh-agent did something wrong: I assume that ssh-agent screws up decryption when auto-adding keys.

My solution to prevent this:

  • Move all private keys out ot ~/.ssh
  • reboot
  • Generate ssh-keypair, don't store in ~/.ssh!
  • Encrypt private key using openssl, "as usual"
  • Move encrypted private key to ~/.ssh
  • ssh-add /path/to/file your private key

You should now be prompted to type in the encryption password.

IMPORTANT:

  • Don't use filenames of keys that were already in the ssh-agent. It seems to try auto-adding known filenames again and again, causing this horrible fail.
  • Don't use private keys with known fingerprint. ssh-agent somehow also auto- added those...
  • Make sure there are absolutely NO private keys in ~/.ssh before doing these steps
  • Make sure there are absolutely NO private keys cached by ssh-agent

If there are any keys left in ~/.ssh or the ssh-agent, it will fail. Once the key has been correctly added with ssh-add, being prompted for your encrpytion password, having no other keys in ~/.ssh or the ssh-agent, it seems to be fine to add other keys.

Sometimes I just HATE linux, for some special (sometimes legacy) packets doing this kind of stuff :(

2
  • Did I get this right: you generated a key successfully, you added the key successfully to your agent, but the server (having the correct public key) does not accept the key from the agent?
    – michas
    Jun 5, 2013 at 22:13
  • Exactly. Keys that were not encrypted using pkcs#8 (the openssl command) work absolutely fine. I can still convert them back using openssl pkcs8 -in id_rsa -out id_rsa_plain, and those keys work fine. pkcs#8 encrypted keys do not work fine with ssh-agent. So either something is wrong with the way I encrypt the key, or something's wrong with the way ssh-agent handles it. As I said, ssh-agent doesn't even prompt for a password to decrypt the key, so I think ssh-agent might not support pkcs#8 encrypted keys?
    – NoMad
    Jun 6, 2013 at 2:19

2 Answers 2

4

OK, to recap what I think happens when adding a pkcs#8 private key, which leads to failure:

  1. Generate a keypair using ssh-keygen (no passphrase)
  2. Encrypt using openssl pkcs8 -topk8
  3. If the encrypted identity file is stored in ~/.ssh along with the matching public key, ssh-agent will add it to its keyring
  4. ssh-agent won't prompt you to decrypt the key, perhaps thinking it's unencrypted?
  5. When trying to connect, ssh-agent can't sign anything with the private key because it's still encrypted

Somthing with ssh-agent's auto-key-adding feature seems to screw up. When a pkcs#8 encrypted private key is automatically added by ssh-agent, it's not decrypting it.

Workaround

For new keys:

  1. Use ssh-keygen to genereate a keypair, do not save them in ~/.ssh!
  2. Encrypt the key using openssl pkcs8 -topk8 -in ~/id_rsa -out ~/id_rsa_enc -v2 des3

To fix existing keys:

  1. Move all private and public keys out of ~/.ssh
  2. Check with ssh-add -l, use -d to delete if neccesary

Either way:

  1. Move only the private key: mv ~/id_rsa_enc ~/.ssh/id_rsa
  2. Add this to ssh-agent: ssh-add
  3. You will now be prompted to enter your passphrase, enter it
  4. Check if the private key was added successfully using ssh-add -l
  5. Move your public key: mv ~/id_rsa.pub ~/.ssh/id_rsa.pub
  6. Check the public key with ssh-add -L

An additional ssh-agent -k or reboot might be useful. Please let me know if this works for you.

0

In theory it should not matter how you encryped the key as the agent stores the unencrypted key, which should be the same in both cases.

Use ssh-add -L to check the correct public key after adding each of the two differently encrypted keys.

Are the two outputs identically and match the public key stored on the server?

3
  • That's why I suspect that ssh-agent doesn't properly decrypt the private key when auto-adding it.
    – NoMad
    Jun 6, 2013 at 12:53
  • That's why I suggest to check ssh-add -L. ;)
    – michas
    Jun 6, 2013 at 12:55
  • See this log, note that I was never prompted by ssh-agent to enter any passphrase to decrypt the encrypted key. It simply can't store it unencrypted without knowing the passphrase... pastebin.com/gFwXC6vF
    – NoMad
    Jun 6, 2013 at 13:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .