Lessons in key management in SSH (v2): Which may use RSA or DSS for its public-keys. ----------------------------------------------------------------------------------------- 1. password-based authentication: The server sends its "host (server) public key" which in theory you have in a user client database somewhere: On unix: ~jrb/.ssh/known_hosts: entry for hahaha.cs.pdx.edu hahaha.cs.pdx.edu,131.252.215.15 ssh-dss AAAAB3NzaC1kc3MAAACBAPdBAVL5Kx1J/prhvtyTkoLr5xh/BH8/LAJ+oP ywQeIhGemOW5ugUL2/E4ExIFu419R0xvxC2Dj4U7nSq4knkP38giMUNTTYOSFnXEnZ+vL1IsKN7Oz0AzatWc+UQFDSOLgPwxVZ qSGgaSRSv971ajHrTknc7STUxtV6ZB0eVNvzAAAAFQDgSMnN9xVx4LbL93dRDZJStbgBdQAAAIB+WzTHavAcp3DZ1L+vXNcqZxKVgSW8ZUd6KQ0mM0XRlY6ZSkBoLmOvFVrH2krAImnG/+mH1b8e3SaDgkLShBQ0BEx608HupXqMlf5VMhnPpfGslRTbYYHDe5nyN+C1q4eaa6kT8V2n8L+tOH3PqD72niyWnHkLlUVT9fTVOwpn7QAAAIEAjPqO3mgYIIF3+Qm0qhls8kxLk1sQyKOb9G5c0wXJ2iUm9s75IEMa7U2tsz+jsbv0Sm+oxAnkOkOaxwNZ80azMRSuC5YK51FV/jHpii63SaRQw0E99Iwaa/2CXD+KczxdVmnZlXgLx6W9hF3ezru0YfRv0FQdu3zcvcadAaZ0HNM= known_hosts is a list of concatenated public keys. ip after ip ... dns:ip key-type key On the server (hahaha) we look at /etc/ssh/ssh_host_dsa_key.pub and find out that it matches the above. Note: 1. We could handcarry that entry ... and create a ssh entry (this is a good thing to learn how to do). 2. We should have a fingerprint of that entry stashed somewhere (on a slip of paper in your wallet). Note these defaults in the server-side /etc/sshd_config: # HostKey for protocol version 1 #HostKey /etc/ssh/ssh_host_key # HostKeys for protocol version 2 #HostKey /etc/ssh/ssh_host_dsa_key /etc/ssh on server: -rw------- 1 root wheel 668 Feb 7 2002 ssh_host_dsa_key - V2 DSS private -rw-r--r-- 1 root wheel 614 Feb 7 2002 ssh_host_dsa_key.pub -rw------- 1 root wheel 539 Feb 7 2002 ssh_host_key - V1 -rw-r--r-- 1 root wheel 343 Feb 7 2002 ssh_host_key.pub -rw------- 1 root wheel 887 May 2 2003 ssh_host_rsa_key - V2 RSA private -rw-r--r-- 1 root wheel 232 May 2 2003 ssh_host_rsa_key.pub Now let's look at ssh-keygen: #man ssh-keygen -t type: for V2, rsa or dsa -p may be used for putting in a passphrase, or a NULL passphrase NULL passphrases are used for automated ssh (typically done with RSA authentication, not password-based authentication). Or for generating the host (server) keys. FreeBSD does the following at boot: case ${sshd_enable} in [Yy][Ee][Ss]) if [ -x /usr/bin/ssh-keygen ]; then if [ ! -f /etc/ssh/ssh_host_key ]; then echo ' creating ssh1 RSA host key'; /usr/bin/ssh-keygen -t rsa1 -N "" \ -f /etc/ssh/ssh_host_key fi if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then echo ' creating ssh2 RSA host key'; /usr/bin/ssh-keygen -t rsa -N "" \ -f /etc/ssh/ssh_host_rsa_key fi if [ ! -f /etc/ssh/ssh_host_dsa_key ]; then echo ' creating ssh2 DSA host key'; /usr/bin/ssh-keygen -t dsa -N "" \ -f /etc/ssh/ssh_host_dsa_key fi fi ;; esac Let's say we log into hahaha.cs.pdx.edu as root and recreate our default dsa key pair: # /usr/bin/ssh-keygen -t dsa -N "" -f /etc/ssh/ssh_host_dsa_key (and kill and restart sshd ... so that it gets its new key, else it pays no attention) (I removed the dss keys and then ran ssh-keygen) hahaha# ssh-keygen -t dsa -N "" -f /etc/ssh/ssh_host_dsa_key Generating public/private dsa key pair. Your identification has been saved in /etc/ssh/ssh_host_dsa_key. Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub. The key fingerprint is: 76:8b:9c:e8:4f:aa:18:63:ad:60:0a:92:19:c1:11:69 root@hahaha.cs.pdx.edu We now have: hahaha# cat ssh*dsa*pub ssh-dss AAAAB3NzaC1kc3MAAACBAOCSE+mvtnJ8XSpx+OUMjqDD/rUGYMzoCx7OE1qpj8tKict7zVbVhVusKbM8SdGz0to2difuz 4R6r2NVJWHbQ5ryb6JGZtbrGQ/rfzdO75vHzqqvwpau+bWT2dLwS4Ree67SsW13JEanVxZ6r0XRTymhz99HDplZrJDOAqe848oPAAAAFQDTivk4YcScslfYCqJEJgtJX5N1tQAAAIBCEJjMkSa53pFJsRHrut5BioruxEQcR6zfEKTXAKnPU5vMwbPeM27W0LrvvLifLmQ+l6/ocUe/BcYWBWFrIBcAkKlISRFECTPaxTL3GjSM6fIsV8DPeXXzxQSzpAX1omCgHkGgQYqPMXF5V68TvWvYhPwVlsf2uQdFldpWZ9LL7AAAAIEAi7cfglLSNFSMmcne5QtuDKhc5bp3KOj59L3JJXQbmCXnv7IIZafZq/JZajWUD8pkaClcpX4QF1AjgbX7DZVqf3Dw/rntw0OIFEtYw5K8ogN+8X/+TwxGWKsgDvb6Sc9p0POTZqd9f0eY+NOGecrk1oJrwTKkigHYO21BM4eV+qQ= root@hahaha.cs.pdx.edu Note: A public key fingerprint is a message digest of the public key (count the bytes above). hahaha# ssh-keygen -l Enter file in which the key is (/root/.ssh/id_rsa): /etc/ssh/ssh_host_dsa_key.pub 1024 76:8b:9c:e8:4f:aa:18:63:ad:60:0a:92:19:c1:11:69 /etc/ssh/ssh_host_dsa_key.pub The old fingerprint was: 1024 70:fe:44:e3:7e:a6:d7:ec:ab:39:4f:05:e8:dc:6e:32 /etc/ssh/ssh_host_dsa_key.pub Now we login to hahaha.cs.pdx.edu, again from somewhere else. Script started on Tue May 18 11:00:00 2004 sh-2.02$ slogin -l root hahaha.cs.pdx.edu @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that the DSA host key has just been changed. The fingerprint for the DSA key sent by the remote host is 76:8b:9c:e8:4f:aa:18:63:ad:60:0a:92:19:c1:11:69. Please contact your system administrator. Add correct host key in /u/jrb/.ssh/known_hosts to get rid of this message. Offending key in /u/jrb/.ssh/known_hosts:3 Password authentication is disabled to avoid man-in-the-middle attacks. Keyboard-interactive authentication is disabled to avoid man-in-the-middle attacks. X11 forwarding is disabled to avoid man-in-the-middle attacks. Permission denied (publickey,password,keyboard-interactive). sh-2.02$ exit Now what? --------------------------------------------------------------------------- In general when you use password-based authentication you face 3 states: 1. the client side has NO key 2. the client side has the WRONG public key (as above) 3. the client side public key matches Given the above, let's go ahead and remove our incorrect .ssh/known_hosts entry (note ssh uses the good old rsh format ... one ASCII line per host, just delete the line) Script started on Tue May 18 11:12:07 2004 sh-2.02$ slogin -l root hahaha.cs.pdx.edu Warning: Permanently added 'hahaha.cs.pdx.edu,131.252.215.15' (DSA) to the list of known hosts. Password: HERE BE DRAGONS What should/could be done here? (hint: how could you reuse ssh-keygen to check the signature?) ----------------------------------------------------------------------------------------- 2. what if we want to use RSA-based authentication. This means you are basically logging in using RSA public/private keys as the authenticator. You must pre-distribute your client key to the server. You also use a passphrase as the first option to protect the private key. In other words, you do NOT use a password. Since I want to login as root on hahaha.cs.pdx.edu I start there... (client)(~) ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (~jrb/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in ~jrbroot/.ssh/id_rsa. Your public key has been saved in ~jrbroot/.ssh/id_rsa.pub. The key fingerprint is: 63:43:00:2d:29:af:93:61:0e:03:8a:28:92:b1:c5:97 jrb@SOMEWHERECLIENTLIKE.pdx.edu There is no way to recover the passphrase. (as usual with passwords). It can however be changed with ssh-keygen. Now I have 3 files ... in .ssh id_rsa id_rsa.pub known_hosts We now must take id_rsa.pub and transfer it to the target system I put it in ~jrb/.ssh/authorized_keys or ~root/.ssh/authorized_keys depending on how I wish to login (which user) How I xfer it is irrelevant as it is a public key. But using ssh with normal public key/password authentication will work. (hahaha)(~/.ssh) ls authorized_keys id_rsa id_rsa.pub known_hosts (client)(~/.ssh) slogin -l root hahaha.cs.pdx.edu slogin -l root hahaha.cs.pdx.edu Enter passphrase for key '/u/jrb/.ssh/id_rsa': The private key on the local client is working with the public key on the server. ----------------------------------------------------------------------------------------- 3. what if we want to automate a task say with scp 1. use RSA authentication (password authentication would mean an ASCII plaintext password in a shellscript ... this is probably not a good idea). 2. use a NULL passphrase. 3. avoid the root account ... one might even create a new account with NO useful password for the task at hand. (minimize privilege). Your decision goes here. Copy a file from here to there but no passphrase. Simple ... when 1. you setup the key, put in a NULL passphrase 2. or setup the key E.g., on the client side to NULL out the passphrase: % ssh-keygen -p -N "" Now % scp foo jrb@hahaha.cs.pdx.edu:foo.txt Will work without a passphrase.